英文:
RegEx expression is not matching the string as expected
问题
我有大量的文本行,我想在notepad++中使用正则表达式进行解析。
以下是我正在处理的一些示例行(我稍微修改了一下,以确保我不会意外显示任何敏感数据):
set routing-instances Vgege443rt3t interface xe-1/3/0.239
set routing-instances Vhr56yjmj interface xe-1/3/0.242
set routing-instances Vm6uj5hvegfee interface xe-1/3/0.243
set routing-instances Vyt3ety34nth5rh interface xe-1/3/0.244
set routing-instances Vk6jrtgrghrege interface xe-1/3/0.245
set routing-instances Vretgetyjygrefef interface xe-1/3/0.246
set routing-instances Vg4etghhthy interface xe-1/3/0.247
set routing-instances V56u5hh5hgdfg interface xe-1/3/0.252
set routing-instances Vmjyuj6jg interface xe-1/3/0.253
set routing-instances Vrtg4t4yg interface xe-1/3/0.255
set routing-instances Vg4ty4hrh interface xe-1/3/0.256
set routing-instances V67ujjthjg interface xe-1/3/0.257
set routing-instances Vgfewfwffmklfpom4 interface xe-1/3/0.258
现在我只想隔离以'V'开头的字符串/单词。因此,例如查看上面的前几行,我要隔离的字符串如下:
Vgege443rt3t
Vhr56yjmj
Vm6uj5hvegfee
Vyt3ety34nth5rh
为了在notepad++中实现这一点,我应用了以下正则表达式:
V.+\s
我相信这会捕获我感兴趣的单个字符串。
然而,当我这样做时,它捕获了我想要的更多内容。请参见我应用此正则表达式时它捕获的一些示例:
Vgege443rt3t interface xe-1/3/0.239
Vhr56yjmj interface xe-1/3/0.242
Vm6uj5hvegfee interface xe-1/3/0.243
Vyt3ety34nth5rh interface xe-1/3/0.244
Vk6jrtgrghrege interface xe-1/3/0.245
显然,它从'V'开始了模式,这正是我想要的。但是,与我认为 \s 会达到的停止模式在第一个空格处不同,它似乎只是继续到行的末尾。
有人能指导一下导致这个问题的正则表达式中的不准确之处吗?
英文:
I have a large number of lines of text that I want to parse in notepad++ using regular expressions.
See some examples of the lines I am working with below (which I have modified a little bit to ensure I don't accidentally show any sensitive data):
set routing-instances Vgege443rt3t interface xe-1/3/0.239
set routing-instances Vhr56yjmj interface xe-1/3/0.242
set routing-instances Vm6uj5hvegfee interface xe-1/3/0.243
set routing-instances Vyt3ety34nth5rh interface xe-1/3/0.244
set routing-instances Vk6jrtgrghrege interface xe-1/3/0.245
set routing-instances Vretgetyjygrefef interface xe-1/3/0.246
set routing-instances Vg4etghhthy interface xe-1/3/0.247
set routing-instances V56u5hh5hgdfg interface xe-1/3/0.252
set routing-instances Vmjyuj6jg interface xe-1/3/0.253
set routing-instances Vrtg4t4yg interface xe-1/3/0.255
set routing-instances Vg4ty4hrh interface xe-1/3/0.256
set routing-instances V67ujjthjg interface xe-1/3/0.257
set routing-instances Vgfewfwffmklfpom4 interface xe-1/3/0.258
Now all I want to do is isolate the string/word that begins with 'V'. So looking at the first few lines above for example, the strings I am looking to isolate are as below:
Vgege443rt3t
Vhr56yjmj
Vm6uj5hvegfee
Vyt3ety34nth5rh
In order to achieve this within notepad++ I applied the below regular expression:
V.+\s
which I believed would capture just the single string I am interested in.
However, when I did this, it captured more than I wanted. See below a couple of examples of what this regex expression captured when I applied it:
Vgege443rt3t interface xe-1/3/0.239
Vhr56yjmj interface xe-1/3/0.242
Vm6uj5hvegfee interface xe-1/3/0.243
Vyt3ety34nth5rh interface xe-1/3/0.244
Vk6jrtgrghrege interface xe-1/3/0.245
So obviously, it started the pattern from the 'V' which is what I had wanted. But rather than stopping the pattern at the first blank space which is what I thought the \s would achieve, it looks to have just continued on to the end of the line.
Can anyone advise what is the inaccuracy within my regex that is causing this problem?
答案1
得分: 1
在Notepad++中,您可以使用以下正则表达式仅匹配:
\b
用于防止部分单词匹配的单词边界V\S*
匹配V
字符和可选的非空白字符(?=\h)
正向先行断言,要求右侧是水平空白字符
如果在 V
字符之前可能没有非空白字符,且您只想匹配单词字符,可以使用以下正则表达式:
(?<!\S)V\w*(?=\h)
英文:
In Notepad++ you could get a match only with:
\bV\S*(?=\h)
Explanation
\b
A word boundary to prevent a partial word matchV\S*
Match aV
char and optional non whitespace chars(?=\h)
Positive lookahead, assert a horizontal whitespace char to the right
If there could be for example no non whitespace chars before the V
char, and you want to match only word characters:
(?<!\S)V\w*(?=\h)
答案2
得分: 0
尝试这个:(V[^\s]+)\s
这应该会匹配以V开头的任何非空格字符,直到第一个空格字符。
英文:
Try this: (V[^\s]+)\s
That should capture V followed by anything that's not a space, up to the first space character.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论