我正在创建一个简单的IDE使用JTextPane
和检测关键字并着色它们.
目前,我能够检测到:
我检测这些类型的方式是通过正则表达式.
现在,我试图检测像[int x = 10;] 这样的变量并将它们着色为不同的颜色.
目前,我能够使用以下正则表达式获取所有数据类型,如int,float char:
Pattern words = Pattern.compile(\\bint\\b|\\bfloat\\b\\bchar\\b);
Matcher matcherWords = words.matcher(code);
while (matcherWords.find()) {
System.out.print(code.substring(matcherWords.start(), matcherWords.end());
// How to get next word that is a variable?
}
Run Code Online (Sandbox Code Playgroud)
以下是我的程序的示例输出:
我怎么能够检测变量一样a
,b
,c
之后我能察觉int
,float
等等?
我需要将服务器级别的X-Frame选项设置为:
- X-Frame-Options:SAMEORIGIN
- X-Frame-Options:ALLOW-FROM https://example.com/
了解X-Frame选项是互斥的.看到这里.
但是,我的应用程序需要在https://example.com和其SAMEORIGIN中进行构建.
请告知是否有解决方法,同时保留我的应用程序的要求,允许在同一来源 框架并在1个外部网站上框架.
或者这不可能吗?
我正在尝试创建简单的IDE并基于我的JTextPane着色
我为源代码着色的方法是覆盖StyledDocument中的insertString和removeString方法.
经过多次测试,我已经完成了评论和关键词.
Q1:至于我的Strings着色,我根据这个正则表达式为我的字符串着色:
Pattern strings = Pattern.compile("\"[^\"]*\"");
Matcher matcherS = strings.matcher(text);
while (matcherS.find()) {
setCharacterAttributes(matcherS.start(), matcherS.end() - matcherS.start(), red, false);
}
Run Code Online (Sandbox Code Playgroud)
这种方式有99%的时间可用,除非我的字符串包含一个特定类型的字符串,其中有一个"\代码内部.这会弄乱我的整个颜色编码.任何人都可以纠正我的正则表达式来修复我的错误吗?
Q2:对于整数和十进制着色,基于此正则表达式检测数字:
Pattern numbers = Pattern.compile("\\d+");
Matcher matcherN = numbers.matcher(text);
while (matcherN.find()) {
setCharacterAttributes(matcherN.start(), matcherN.end() - matcherN.start(), magenta, false);
}
Run Code Online (Sandbox Code Playgroud)
通过使用正则表达式"\ d +",我只处理整数而不是浮点数.此外,作为另一个字符串的一部分的整数是匹配的,这不是我想要的IDE内部.哪个是用于整数颜色编码的正确表达式?
以下是输出的屏幕截图:
感谢您提前帮助!
我了解可以使用JavaCompiler通过字符串来编译Java源代码。使用包含我的Java代码的长字符串,我可以测试我的代码是否可编译。
来源:http : //docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html
范例:http://www.java2s.com/Code/Java/JDK-6/CompileaJavafilewithJavaCompiler.htm
我的问题是:我有一个长字符串,其中包含可编译的C ++代码。我是否可以使用某种形式的Java库执行类似的操作?
谢谢
编辑1:根据要求,字符串可以由用户生成(在GUI-JTextArea中键入)或从.cpp文件读取。
是否可以使用javascript重定向URL的主机名?
如果URL包含“内容/文章”,则应保留在相同的URL中。否则,它将所有其他URL从www重定向到www1。
我想我得到了“ / content / articles”部分,但window.location.replace似乎不起作用。
例如:
<script type="text/javascript">
window.onload = function() {
if (window.location.href.indexOf("/content/articles") > -1) {
// Do not redirect
} else {
// Redirect from www to www1
window.location.replace(window.location.protocol + "//" + window.location.hostname.replace("www", "www1")+window.location.pathname);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我正在尝试验证我的数据库中的数字数据。
我是否能够检测到检索到的数据是以 8 还是 9 开头?
例如:
8999999 有效
9999999 有效
7000000 无效
谢谢
java ×3
regex ×2
c++ ×1
compilation ×1
html ×1
iframe ×1
javascript ×1
jtextpane ×1
oracle ×1
redirect ×1
validation ×1
variables ×1
webserver ×1