正则表达式,匹配特定字符之前和之后的部分。

huangapple go评论65阅读模式
英文:

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+|(?=[(:)])|(?<=[(:])

正则表达式演示 | Java 演示

示例代码

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+|(?=[(:)])|(?&lt;=[(:])

Regex demo | Java demo

Example code

String s = &quot;foo(a:b)&quot;;
Scanner scanner = new Scanner(s).useDelimiter(&quot;\\s+|(?=[(:)])|(?&lt;=[(:])&quot;);
while(scanner.hasNext())
{
    System.out.println(scanner.next());
}

Output

foo
(
a
:
b
)

huangapple
  • 本文由 发表于 2020年9月18日 20:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定