[英]Python extract occurence of a string with regex
I need a python regular expression to extract all the occurrences of a string from the line . 我需要一个python正则表达式来从行中提取所有出现的字符串。
So for example, 例如
line = 'TokenRange(start_token:5835456583056758754, end_token:5867789857766669245, rack:brikbrik0),EndpointDetails(host:192.168.210.183, datacenter:DC1, rack:brikbrikadfdas), EndpointDetails(host:192.168.210.182, datacenter:DC1, rack:brikbrik1adf)])'
I want to extract all the string which contains the rack ID. 我想提取所有包含机架ID的字符串。 I am crappy with reg ex, so when I looked at the python docs but could not find the correct use of re.findAll or some similar regex expression. 我对reg ex很不满意,因此当我查看python文档时,却找不到re.findAll或某些类似的regex表达式的正确用法。 Can someone help me with the regular expression? 有人可以帮我提供正则表达式吗? Here is the output i need : [brikbrik0,brikbrikadfdas, brikbrik1adf] 这是我需要的输出:[brikbrik0,brikbrikadfdas,brikbrik1adf]
You can capture alphanumerics coming after the rack:
: 您可以捕获rack:
后面的字母数字:
>>> re.findall(r"rack:(\w+)", line)
['brikbrik0', 'brikbrikadfdas', 'brikbrik1adf']
Add a word boundary to rack
: 在rack
添加单词边界 :
\brack:(\w+)
See a demo on regex101.com . 参见regex101.com上的演示 。
Python
( demo on ideone.com ):
在Python
( ideone.com上的演示 ):
import re
string = """TokenRange(start_token:5835456583056758754, end_token:5867789857766669245, rack:brikbrik0),EndpointDetails(host:192.168.210.183, datacenter:DC1, rack:brikbrikadfdas), EndpointDetails(host:192.168.210.182, datacenter:DC1, rack:brikbrik1adf)])"""
rx = re.compile(r'\brack:(\w+)')
matches = [match.group(1) for match in rx.finditer(string)]
print(matches)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.