英文:
Capture one suffix containing known substring when multiple matching prefixes (without known substring) found
问题
使用以下正则表达式:.*is:\b([Foo|Bar]*)\b.*
并且使用以下示例的测试输入行匹配:
"is:Baz is:Foo FooBar" # 捕获 "Foo"
"is:Foo FooBar is:Bar" # 捕获 "Bar"
"is:Bar FooBar FooBaz Baz" # 捕获 "Bar"
"FooBar is:Bar FooBaz" # 捕获 "Bar"
"FooBar is:Xyzzy is:Foo" # 捕获 "Foo"
"is:Baz FooBar is:Foo" # 捕获 "Foo"
"FooBar is:Foo is:Xyzzy" # 不捕获
在最后一行,我还想捕获is:Foo
,但由于is:Xyzzy
的存在而导致捕获被干扰。这不是所有可能的测试用例的详尽列表,但它说明了我遇到的问题。
英文:
Given an input of multiple string, some containing the prefix is:
, I need to capture one instance of the substring "Foo" or "Bar" following the is:
prefix regardless of how many times is:Foo
/is:Bar
or is:Baz
/is:Xyzzy
appear.
Using the following regex: .*is:\b([Foo|Bar]*)\b.*
And using the following examples of test input lines with matches:
"is:Baz is:Foo FooBar" # Captures "Foo"
"is:Foo FooBar is:Bar" # Captures "Bar"
"is:Bar FooBar FooBaz Baz" # Captures "Bar"
"FooBar is:Bar FooBaz" # Captures "Bar"
"FooBar is:Xyzzy is:Foo" # Captures "Foo
"is:Baz FooBar is:Foo" # Captures "Foo"
"FooBar is:Foo is:Xyzzy" # No capture
In the final line I want to also capture is:Foo
, but the capture is thrown off by is:Xyzzy
. This isn't an exhaustive list of possible test cases but it illustrates to problem I'm coming up against.
答案1
得分: 0
你可以使用分组来编写模式,而无需使用表示字符类的 [
和 ]
。
这里不需要单词边界 :\b
,因为由于后续的交替项 (Foo|Bar)
,它是隐式的。
在 \bis
之前可以添加一个单词边界。
.*\bis:(Foo|Bar)\b.*
查看 regex101 演示。
英文:
You can write the pattern using a grouping without the [
and ]
that denotes a character class.
You don't need a word boundary here :\b
as it is implicit due to the following alternation of (Foo|Bar)
You can append a word boundary before \bis
.*\bis:(Foo|Bar)\b.*
See a regex101 demo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论