哪个是用于数字和字符串的正确正则表达式? [英] Which is the right regular expression to use for Numbers and Strings?
问题描述
我正在尝试创建简单的IDE并根据我的JTextPane着色
I am trying to create simple IDE and coloring my JTextPane based on
- 字符串()
- 评论(//和/ * * /)
- 关键字(public,int ...)
- 数字(69之类的整数)和浮点数如1.5)
- Strings (" ")
- Comments (// and /* */)
- Keywords (public, int ...)
- Numbers (integers like 69 and floats like 1.5)
我为源代码着色的方法是覆盖StyledDocument中的insertString和removeString方法。
The way i color my source code is by overwritting the insertString and removeString methods inside the StyledDocument.
经过大量测试,我已经完成了评论和关键字。
After much testing, i have completed comments and keywords.
Q1:至于我的字符串着色,我为我的字符串着色基于此正则表达式:
Q1: As for my Strings coloring, I color my strings based on this regular expression:
Pattern strings = Pattern.compile("\"[^\"]*\"");
Matcher matcherS = strings.matcher(text);
while (matcherS.find()) {
setCharacterAttributes(matcherS.start(), matcherS.end() - matcherS.start(), red, false);
}
这是有效的99%的时间除了我的字符串包含特定类型的字符串的时候代码中的\。这弄乱了我的整个颜色编码。
任何人都可以更正我的正则表达式来修复我的错误吗?
This works 99% of the time except for when my string contains a specific kind of string where there is a "\ inside the code. This messes up my whole color coding. Can anyone correct my regular expression to fix my error?
Q2:对于整数和小数着色,根据这个正则表达式检测数字:
Q2: As for Integers and Decimal coloring, numbers are detected based on this regular expression:
Pattern numbers = Pattern.compile("\\d+");
Matcher matcherN = numbers.matcher(text);
while (matcherN.find()) {
setCharacterAttributes(matcherN.start(), matcherN.end() - matcherN.start(), magenta, false);
}
通过使用正则表达式\d +,我只处理整数而不是漂浮。此外,作为另一个字符串的一部分的整数是匹配的,这不是我想要的IDE内部。哪个是用于整数颜色编码的正确表达式?
By using the regular expression "\d+", I am only handling integers and not floats. Also, integers that are part of another string are matched which is not what i want inside an IDE. Which is the correct expression to use for integer color coding?
下面是输出的屏幕截图:
Below is a screenshot of the output:
感谢您提前获得任何帮助!
Thank you for any help in advance!
推荐答案
对于字符串,这可能是最快的正则表达式 -
For the strings, this is probably the fastest regex -
\ [^ \ \\\\] *(?: \\\\。[^ \ \\\\] *)* \
格式化:
" [^"\\]*
(?: \\ . [^"\\]* )*
"
对于整数和十进制数,我所知道的唯一万无一失的表达式是
this -
For integers and decimal numbers, the only foolproof expression I know of is
this -
(?:\\d +(?:\\\\\ *)?| \\。\\\\ +)
格式化:
(?:
\d+
(?: \. \d* )?
| \. \d+
)
作为旁注,如果您从
的开头独立完成每个字符串,您可能会重叠突出显示。
As a side note, If you're doing each independently from the start of
the string you could be possibly overlapping highlights.
这篇关于哪个是用于数字和字符串的正确正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!