简体   繁体   English

RegEx用于识别具有特殊字符和边界的字母数字模式

[英]RegEx for identifying alphanumeric patterns with special chars and boundaries

I am attempting to return one of these strings (depends on input): 我试图返回以下字符串之一(取决于输入):

f23/24  /or/  f23-24   /or/  f23+24

(Ideally, it would be great if it ALWAYS returned format f23-24), regardless of input (理想情况下,始终返回格式f23-24会很棒),无论输入如何

from this type of string: 从这种类型的字符串:

build-f23/24 1st pass demo (50:50)   #Should output f23-24 or f23/24
build-f17-22 1st pass demo (50:50)   #Should output f17-22
build-f-1 +14 1st pass demo (50:50)  #Should output f1-14 or f1+14

Exception: 例外:

Some of the strings will not have the second set of numbers: 一些字符串将没有第二组数字:

build-f45 1st pass demo (50:50)      #Should output f45


Where I am currently at: 我目前在的位置:

Thus far, I have this regex, but it always fails if the separator char is a slash : 到目前为止,我有这个正则表达式, 但是如果分隔符char是一个斜杠它总是会失败

regex = r"(\s?)(\-?)(f)(\s?)([\+\-\/]?)(\d\d*)(-?)(\d?\d*)"
tmp = re.search(regex, val)[0]

For your test data, you can try the following regular expression -(f)-?(\\d+)(?:\\s*([-+/]\\d+))? 对于测试数据,您可以尝试以下正则表达式-(f)-?(\\d+)(?:\\s*([-+/]\\d+))? .

import re

val = '''
build-f23/24 1st pass demo (50:50)
build-f17-22 1st pass demo (50:50)
build-f-1 +14 1st pass demo (50:50)
build-f45 1st pass demo (50:50)
'''

expected = [['f23-24', 'f23/24'], ['f17-22'], ['f1-14', 'f1+14'], ['f45']]

for m, x in zip(re.findall(r'-(f)-?(\d+)(?:\s*([-+/]\d+))?', val), expected):
  result = ''.join(m)
  print(result in x, ':', result)

Output: 输出:

True : f23/24
True : f17-22
True : f1+14
True : f45

This is a quite complicated expression, which I'm not sure if I understand the ration, but maybe let's start with an expression to output what is desired, maybe we could solve the problem step by step: 这是一个非常复杂的表达式,我不确定我是否了解比例,但是也许让我们从一个表达式开始输出所需的内容,也许我们可以逐步解决问题:

.+?(-.+?)([a-z][0-9]+?)?\s|(?:[+][0-9])?([0-9]+)?(.+)

Test 测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r".+?(-.+?)([a-z][0-9]+?)?\s|(?:[+][0-9])?([0-9]+)?(.+)"

test_str = ("build-f23/24 1st pass demo (50:50)\n"
    "build-f17-22 1st pass demo (50:50)\n"
    "build-f-1 +14 1st pass demo (50:50)")

subst = "\\1\\2\\3"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

在此处输入图片说明

DEMO 演示

在此处输入图片说明

import re

dat = """build-f23/24 1st pass demo (50:50)
      build-f17-22 1st pass demo (50:50)
      build-f-1 +14 1st pass demo (50:50)
      build-f45 1st pass demo (50:50)"""

rgx = r'(?mi)^.*(?<=-)(f)\D?(\d+)(?:\s?([+\/-]\d+))?.*$'
re.sub(rgx,r'\1\2\3',dat).split()
['f23/24', 'f17-22', 'f1+14', 'f45']

or you could do: 或者你可以做:

rgx1 = r'(?mi)^.*(?<=-)(f)\D?(\d+)(?:\s?[+\/-](\d+))?.*$'
re.sub('(?m)-$','',re.sub(rgx1 ,r'\1\2-\3',dat)).split()
['f23-24', 'f17-22', 'f1-14', 'f45']

or instead of using sub twice you could replace directly: 或代替使用sub两次,您可以直接替换:

re.sub(rgx1,lambda x: f'{x.group(1)}{x.group(2)}-{x.group(3)}' 
                         if x.group(3) else f'{x.group(1)}{x.group(2)}',dat).split()
['f23-24', 'f17-22', 'f1-14', 'f45']

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

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