[英]Regex match strings divided by 'and'
我需要解析一个字符串以获得所需的数字,然后 position 形成一个字符串,例如:
2 Better Developers and 3 Testers
5 Mechanics and chef
medic and 3 nurses
目前我正在使用这样的代码,它返回元组列表,例如[('2', 'Better Developers'), ('3', 'Testers')]
:
def parse_workers_list_from_str(string_value: str) -> [(str, str)]:
result: [(str, str)] = []
if string_value:
for part in string_value.split('and'):
result.append(re.findall(r'(?: *)(\d+|)(?: |)([\w ]+)', part.strip())[0])
return result
我可以在没有.split()
的情况下仅使用正则表达式吗?
与re.MULTILINE
一起,您可以在一个正则表达式中完成所有操作,这也将正确拆分所有内容:
>>> s = """2 Better Developers and 3 Testers
5 Mechanics and chef
medic and 3 nurses"""
>>> re.findall(r"\s*(\d*)\s*(.+?)(?:\s+and\s+|$)", s, re.MULTILINE)
[('2', 'Better Developers'), ('3', 'Testers'), ('5', 'Mechanics'), ('', 'chef'), ('', 'medic'), ('3', 'nurses')]
随着空''
到1
的解释和转换:
import re
s = """2 Better Developers and 3 Testers
5 Mechanics and chef
medic and 3 nurses"""
results = re.findall(r"""
# Capture the number if one exists
(\d*)
# Remove spacing between number and text
\s*
# Caputre the text
(.+?)
# Attempt to match the word 'and' or the end of the line
(?:\s+and\s+|$\n?)
""", s, re.MULTILINE|re.VERBOSE)
results = [(int(n or 1), t.title()) for n, t in results]
results == [(2, 'Better Developers'), (3, 'Testers'), (5, 'Mechanics'), (1, 'Chef'), (1, 'Medic'), (3, 'Nurses')]
你可以使用这个正则表达式:
(\d*) *(\S+(?: \S+)*?) and (\d*) *(\S+(?: \S+)*)
在这里,我们匹配and
在两侧用一个空间包围。 之前和之后and
我们使用这个子模式进行匹配:
(\d*) *(\S+(?: \S+)*?)
匹配可选的 0+ 位开头,后跟 0 个或多个空格,后跟 1 个或多个由空格分隔的非空白字符串。
代码:
import re
arr = ['2 Better Developers and 3 Testers', '5 Mechanics and chef', 'medic and 3 nurses', '5 foo']
rx = re.compile(r'(\d*) *(\S+(?: \S+)*?) and (\d*) *(\S+(?: \S+)*)')
for s in arr: print (rx.findall(s))
Output:
[('2', 'Better Developers', '3', 'Testers')]
[('5', 'Mechanics', '', 'chef')]
[('', 'medic', '3', 'nurses')]
[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.