我正在尝试编写一个Java正则表达式,它将找到2之间的所有字符串:.如果字符之间的字符串有空格,行结尾或制表符,则应忽略它.空字符串也会被忽略._好的!该组可以包括:或不包括.
以下是一些测试和预期的组:
"test :candidate: test" => ":candidate:"
"test :candidate: test:" => ":candidate:"
"test :candidate:_test:" => ":candidate:", ":_test:"
"test :candidate::test" => ":candidate:"
"test ::candidate: test" => ":candidate:"
"test :candidate_: :candidate: test" => ":candidate_:", ":candidate:"
"test :candidate_:candidate: test" => ":candidate_:", ":candidate:"
Run Code Online (Sandbox Code Playgroud)
我测试了很多正则表达式,这些几乎可以工作:
":(\\w+):"
":[^:]+:"
Run Code Online (Sandbox Code Playgroud)
当2组"共享"冒号时,我仍然有问题:
"test :candidate_: :candidate: test" => ":candidate_:", ":candidate:" // OK
"test :candidate_:candidate: test" => ":candidate_:" // ERROR! :(
Run Code Online (Sandbox Code Playgroud)
看起来第一组"消耗"第二个冒号并且匹配器找不到我预期的第二个字符串.
有人能指出我正确的方向来解决这个问题吗?你能详细说明为什么匹配器"消耗"结肠吗?
谢谢.