简体   繁体   English

提取子字符串的正则表达式是什么?

[英]What would be the regular expression to extract substring?

I need to have a regular expression to extract the value of vector_name in different strings. 我需要一个正则表达式来提取不同字符串中vector_name的值。 I tried the followings, but could not get it to work. 我尝试了以下,但无法让它工作。

import re 进口重新

# I want to extract the value of vector_name using a regular expression

mystring1 = "options={}, params={vector_name=get_x, default_like_count=2, seeting_id=1200, system=0 back"
mystring2 = "literal options={}, params={setting_id=1200, seed_val=22, system=0, vector_name=get_Z foobar}"
mystring3 = "params={seed_rand=1200, seed_val=22, system=0, vector_name=corodinate2, tt=lly}"

# I have
re.search(r'vector_name=([^/]+),', mystring1).group(1)
# should give get_x

re.search(r'vector_name=([^/]+),', mystring2).group(1)
# should give get_Z

re.search(r'vector_name=([^/]+),', mystring3).group(1)
# should give corodinate2

Does anyone have any idea what would be the correct regex? 有谁知道什么是正确的正则表达式?

The [^/]+ pattern matches one or more chars other than / greedily. [^/]+模式匹配除/贪婪之外的一个或多个字符。

You may restrict the chars you want to match, say, with \\w+ , to match 1 or more word chars (ie letters, digits, underscores): 您可以使用\\w+限制要匹配的字符,以匹配1个或多个字符(即字母,数字,下划线):

r'vector_name=(\w+)'

See the regex demo 请参阅正则表达式演示

Python demo : Python演示

import re
strs = ['options={}, params={vector_name=get_x, default_like_count=2, seeting_id=1200, system=0 back', 'literal options={}, params={setting_id=1200, seed_val=22, system=0, vector_name=get_Z foobar}', 'mystring3 = "params={seed_rand=1200, seed_val=22, system=0, vector_name=corodinate2, tt=lly}' ]
rx = re.compile(r'vector_name=(\w+)')
for s in strs:
    m = rx.search(s)
    if m:
        print(m.group(1))
# => ['get_x', 'get_Z', 'corodinate2']

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

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