简体   繁体   English

正则表达式匹配无法在python中选择带空格的字母数字字符串

[英]regex matching is unable to select alphanumeric string with spaces in python

I have the following list of expressions in python我在python中有以下表达式列表

LIST1=["AR BR_18_0138249",  "AR R_16_01382649",  "BR 16 0138264", "R 16 01382679" ]

In the above string a few patterns are alpha numeric but there is a space between the two second set of sequences.在上面的字符串中,一些模式是字母数字,但第二组序列之间有一个空格。 I expect the following output我期望以下输出

  "AR BR_18_0138249"
  "AR R_16_01382649"
  "BR 16 0138264"
  "R 16 01382679" 

I have tried the following code我试过下面的代码

import regex as re
pattern = r"(\bB?R_\w+)(?!.*\1)|(\bB?R \w+)(?!.*\1)|(\bR?^sd \w+)(?!.*\1)"
for i in LIST1:
rest = re.search(pattern, i)
if rest:
    print(rest.group(1))

I have obtained the following result我得到了以下结果

BR_18_0138249
R_16_01382649
None
None

I am unable to get the sequences with the spaces.我无法获得带有空格的序列。 I request someone to guide me in this regard我请求有人在这方面指导我

You can use您可以使用

\b(B?R(?=([\s_]))(?:\2\d+)+)\b(?!.*\b\1\b)

See the regex demo查看正则表达式演示

Details细节

  • \\b - a word boundary \\b - 单词边界
  • (B?R(?=([\\s_]))(?:\\2\\d+)+) - Group 1: an optional B , then R , then one or more sequences of a whitespace or underscore followed with one or more digits (if you need to support letters here, replace \\d+ with [^\\W_] ) (B?R(?=([\\s_]))(?:\\2\\d+)+) - 第 1 组:可选的B ,然后是R ,然后是一个或多个空格或下划线序列,后跟一个或多个数字(如果您需要在此处支持字母,请将\\d+替换为[^\\W_]
  • \\b - a word boundary \\b - 单词边界
  • (?!.*\\b\\1\\b) - a negative lookahead that fails the match if there are (?!.*\\b\\1\\b) - 如果有
    • .* - any zero or more chars other than line break chars, as many as possible .* - 除换行符以外的任何零个或多个字符,尽可能多
    • \\b\\1\\b - the same value as in Group 1 matched as a whole word (not enclosed with letters, digits or underscores). \\b\\1\\b - 与第 1 组中相同的值作为整个单词匹配(不包含字母、数字或下划线)。

See a Python re demo (you do not need the PyPi regex module here):请参阅Python re演示(此处不需要 PyPi 正则表达式模块):

import re
LIST1=["AR BR_18_0138249",  "AR R_16_01382649",  "BR 16 0138264", "R 16 01382679" ]
pattern = r"\b(B?R(?=([\s_]))(?:\2\d+)+)\b(?!.*\b\1\b)"
for i in LIST1:
  rest = re.search(pattern, i)
  if rest:
    print(rest.group(1))

This does the work:这做的工作:

[A-Z]{1,2}\s([A-Z]{1,2}+(?:_[0-9]+)*|[0-9]+(?:\s[0-9]+)*)

This regex gives below output:此正则表达式提供以下输出:

AR BR_18_0138249
AR R_16_01382649
BR 16 0138264
R 16 01382679

See demo here在这里查看演示

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

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