简体   繁体   English

正则表达式-在多次出现的定界符之间匹配每个项目

[英]Regex - Match each item between multiple occurrences of delimiter

I would like to match the values between a given delimiter using a Regex expression in Python. 我想在Python中使用Regex表达式匹配给定分隔符之间的值。 I would like this to ignore surrounding white space. 我想忽略周围的空白。 For example: 例如:

  • The string 1, b cc, "a" and delimiter , will return three matches of 1 , b cc , "a" 字符串1, b cc, "a"和delimiter ,将返回三个匹配项1b cc"a"

  • The string 4 + 5 + 2 +1 and delimiter + will return four matches of 4 , 5 , 2 , 1 字符串4 + 5 + 2 +1和定界符+将返回四场比赛的4521

import re
line = '1, b cc, "a"'
re.split(r'[;,]\s*', line)
Out[7]: ['1', 'b cc', '"a"']

line = '4 +   5 +   2  +1'
re.split(r'\s*[+]\s*', line)
Out[10]: ['4', '5', '2', '1']

The re.split() function is useful because you can specify multiple patterns for the separator. re.split()函数很有用,因为您可以为分隔符指定多个模式。

In this case, for your first request, the separator is either a comma (,) , semicolon (;) , followed by any amount of extra whitespace . 在这种情况下,对于您的第一个请求,分隔符可以是comma (,)semicolon (;) ,后跟任意数量的多余whitespace For your second request, the separator is plus (+) , surrounded by any amount of extra whitespaces . 对于您的第二个请求,分隔符是plus (+) ,并由任意数量的多余whitespaces包围。

Whenever that pattern is found, the entire match becomes the delimiter between whatever fields lie on either side of the match. 只要找到该模式,整个比赛就成为比赛双方任何字段之间的分隔符。 The result is a list of fields, just as with str.split() 结果是一个字段列表,与str.split()

You can do this with the re.split() method. 您可以使用re.split()方法执行此操作。

import re

re.split('\s*,\s*', '1, b cc, "a"')

re.split('\s*\+\s*', '4 +   5 +   2  +1')

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

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