英文:
Regex that matches before and after certain characters
问题
我正在尝试创建一个分隔符正则表达式(用于 java.util.Scanner),它可以在空白字符上进行分段,同时将冒号、开括号和闭括号保留为单独的标记。也就是说,foo(a:b)
应该被分割为标记 foo
、(
、a
、:
、b
和 )
。
我目前最好的尝试是模式 "\\s+|(?=[(:])|(?<=[:)])"
,由于某种我无法理解的原因,在开括号之后和闭括号之前无法匹配,但在冒号的两边可以匹配成功。
英文:
I am trying to craft a delimiter regex (for use with java.util.Scanner) that segments a string on whitespace, as well as keeping colons, opening parenthesis and closing parenthesis as separate tokens. That is, foo(a:b)
should segment into the tokens foo
, (
, a
, :
, b
and )
.
My current best effort is the pattern "\\s+|(?=[(:])|(?<=[:)])"
which for some reason I can't understand fails to match after the opening parenthesis and before the closing parenthesis, but matches fine on both sides of the colon.
答案1
得分: 2
如果您想要这些单独的部分,您可以扩展字符类,将字符 [(:)]
之一断言为左侧字符,如果这是整个字符串,则在右侧断言字符 [(:]
。
如果您还想匹配最后一个闭括号后的位置,两个字符类可以相同 [(:)]
\s+|(?=[(:)])|(?<=[(:])
示例代码
String s = "foo(a:b)";
Scanner scanner = new Scanner(s).useDelimiter("\\s+|(?=[(:)])|(?<=[(:])");
while(scanner.hasNext())
{
System.out.println(scanner.next());
}
输出
foo
(
a
:
b
)
英文:
If you want all those separate parts, you could extend the character classes asserting one of the characters [(:)]
at the left and, if this is the whole string, assert one of the characters [(:]
at the right.
If you also want to match the position after the last closing parenthesis, both character classes can be the same [(:)]
\s+|(?=[(:)])|(?<=[(:])
Example code
String s = "foo(a:b)";
Scanner scanner = new Scanner(s).useDelimiter("\\s+|(?=[(:)])|(?<=[(:])");
while(scanner.hasNext())
{
System.out.println(scanner.next());
}
Output
foo
(
a
:
b
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论