正则表达式未按预期匹配字符串。

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

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) 正向先行断言,要求右侧是水平空白字符

Regex101演示

如果在 V 字符之前可能没有非空白字符,且您只想匹配单词字符,可以使用以下正则表达式:

  • (?<!\S)V\w*(?=\h)

Regex101演示

英文:

In Notepad++ you could get a match only with:

\bV\S*(?=\h)

Explanation

  • \b A word boundary to prevent a partial word match
  • V\S* Match a V char and optional non whitespace chars
  • (?=\h) Positive lookahead, assert a horizontal whitespace char to the right

Regex101 demo.

If there could be for example no non whitespace chars before the V char, and you want to match only word characters:

(?&lt;!\S)V\w*(?=\h)

Regex101 demo.

答案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.

huangapple
  • 本文由 发表于 2023年2月14日 00:45:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438828.html
匿名

发表评论

匿名网友

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

确定