how can i extract bold range string in below
我怎样才能在下面提取粗体字符串
string :
字符串:
- hello world blah -d blah vlaah -n blah vlahh
- 你好世界blah -d blah vlaah -n blah vlahh
- hello world blah -n blah vlahh -d blah vlaah
- 你好世界blah -n blah vlahh -d blah vlaah
- hello world blah -d blaaah
- 你好世界blah -d blaaah
I tried. -[dn] .*$
but it found longest match string like below
我试过了。 - [dn]。* $但它找到了如下所示的最长匹配字符串
- hello world blah -d blah vlaah -n blah vlahh
- 你好世界blah -d blah vlaah -n blah vlahh
I want to extract shortest match string . thanks in advance
我想提取最短的匹配字符串。提前致谢
2 个解决方案
#1
5
You can use a negative lookahead to avoid matching another -d/-n
in the match:
您可以使用负向前瞻以避免匹配匹配中的另一个-d / -n:
-[dn] (?!.*?-[dn]).*$
RegEx演示
#2
2
Could throw a greedy .*
before to eat up:
可能会贪婪。*吃饭之前:
^.*(-[dn] .*)$
And grab matches of the first capture group. See test at regex101
并抓住第一个捕获组的匹配。请参阅regex101上的测试
#1
5
You can use a negative lookahead to avoid matching another -d/-n
in the match:
您可以使用负向前瞻以避免匹配匹配中的另一个-d / -n:
-[dn] (?!.*?-[dn]).*$
RegEx演示
#2
2
Could throw a greedy .*
before to eat up:
可能会贪婪。*吃饭之前:
^.*(-[dn] .*)$
And grab matches of the first capture group. See test at regex101
并抓住第一个捕获组的匹配。请参阅regex101上的测试