简体   繁体   English

正则表达式查找完全匹配

[英]Regex to find exact match

I have the following scenarios:我有以下场景:

message: ON
message: "ON"
message: 'ON'
message: "TONC"
message: Los Angeles, ON
message: HOLDS ON FILE

I want to match the word ON in the the first three scenarios, and not the others.我想在前三个场景中匹配 ON 这个词,而不是其他场景。

I tried with positive/negative lookaheads/behinds to no avail.我尝试了积极/消极的前瞻/落后无济于事。 ex:前任:

\bON\b(?=[\s{0,1}\"{0,1}\'{0,1}]$)

I recommend munging the string to eliminate the stuff you don't want rather than trying to match directly.我建议修改字符串以消除您不想要的东西,而不是尝试直接匹配。 Here I'm just removing non-alpha characters and comparing against the desired result:在这里,我只是删除非字母字符并与所需的结果进行比较:

#!/usr/bin/env python3
import re

tests = ['message: ON',
'message: "ON"',
'message: \'ON\'',
'message: "TONC"',
'message: Los Angeles, ON',
'message: HOLDS ON FILE']

for test in tests:
  test = re.sub(r'\W+', '', test)
  print(test)
  print(test == "messageON")

You may use您可以使用

r'''(?<=:\s)(["']?)\bON\b\1'''

See the regex demo .请参阅正则表达式演示 If necessary, add $ at the end of the pattern to make it match at the end of the string.如有必要,在模式末尾添加$以使其与字符串末尾匹配。

Details细节

  • (?<=:\s) - a positive lookbehind that matches a location that is immediately preceded with : and a whitespace char (?<=:\s) - 与紧接在:和空白字符前面的位置匹配的正向后视
  • (["']?) - Group 1: an optional " or ' char (["']?) - 第 1 组:可选的"'字符
  • \bON\b - whole word ON \bON\b - 整个字ON
  • \1 - Backreference to Group 1 value, either " or ' . \1 - 对第 1 组值的反向引用,可以是"'

NOTE : If there can be a variable amount of whitespaces between : and the ON word, you may use PyPi regex module and use r'''(?<=:\s*)(["']?)\bON\b\1''' pattern.注意:如果:ON字之间可以有可变数量的空格,您可以使用PyPi正则表达式模块并使用r'''(?<=:\s*)(["']?)\bON\b\1'''模式。

Why not为什么不

message: (['"])?ON(?(1)\1)

See a demo on regex101.com .请参阅regex101.com 上的演示

If you only want to match only ON , another option is to use an alternation matching either between a colon followed by a space and a whitespace boundary at the right or between the same type of quotes.如果您只想匹配ON ,另一种选择是在冒号后跟空格和右侧的空白边界之间或在相同类型的引号之间使用交替匹配。

(?<=: )ON(?!\S)|(?<=: (['"]))ON(?=\1)
  • (?<=: ) Asserting : and space on the left (?<=: )断言:和左边的空格
  • ON Match ON ON匹配ON
  • (?!\S) Assert a whitespace boundary on the right (?!\S)在右边断言一个空白边界
  • | Or或者
  • (?<=: (['"])) Assert : and space on the left and capture in group 1 ' or " (?<=: (['"]))断言:和左边的空格并在第 1 组中捕获'"
  • ON Match ON ON匹配ON
  • (?=\1) Assert on the right the same quote that is matched in group 1 using a backreference (?=\1)使用反向引用在右侧断言与第 1 组匹配的相同引号

Regex demo正则表达式演示

I basically just add message: in the regex ( r'^message: ["\']?\bON\b["\']?$' ):我基本上只是在正则表达式中添加message:r'^message: ["\']?\bON\b["\']?$' ):

#!/usr/bin/env python3

from re import search

strings = [
'message: ON',
'message: "ON"',
'message: \'ON\'',
'message: "TONC"',
'message: Los Angeles, ON',
'message: HOLDS ON FILE',
]

for s in strings:
    m = search(r'^message: ["\']?\bON\b["\']?$', s)
    if m is not None:
        print(s)
        print("It's a match !")

and it works, here is the output:它有效,这里是 output:

$ ./regex_to_find_exact_match.py
message: ON
It's a match !
message: "ON"
It's a match !
message: 'ON'
It's a match !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM