0

我想从格式化的字符串中提取特定的子字符串。例如,原始字符串是19592@click(className='android.widget.TextView',instance='15'):android.widget.TextView@"All".

我想从上面的字符串中提取"click", "android.widget.TextView", "15", 。"android.widget.TextView@"All""这是一个可以通过 python regex 解决的问题吗?我不确定应该使用哪些 API。

4

2 回答 2

2

这回答了你的问题了吗?- https://www.w3resource.com/python-exercises/re/python-re-exercise-47.php

否则你可以使用 - https://docs.python.org/3/library/configparser.html

这里配置解析器将解析您的字符串,可以为您提供由分隔符分隔的所有值(@,=,:)

于 2019-12-03T07:04:16.780 回答
0

我知道正则表达式是您所要求的,但也许您可以使用find更有效的普通方法:

def find_between(s, first, last):
    first_pos = s.find(first)
    last_pos = s.find(last)
    if first_pos < 0 or last_pos < 0:
        return None
    first_pos = first_pos + len(first)
    return s[first_pos:last_pos]

接着:

s = '19592@click(className=\'android.widget.TextView\',instance=\'15\'):android.widget.TextView@"All"'
print find_between(s, "@", "(")
Out[0]: 'click'

print find_between(s, "className=", ",")
Out[18]: "'android.widget.TextView'"
于 2019-12-03T07:06:59.620 回答