[英]escape continuously strings in regex matching in python
I met one problem when I use regex to match some string using Python. 当我使用regex使用Python匹配某些字符串时遇到了一个问题。
Example string: 示例字符串:
ln[1] --This is a string-- ln [1]-这是一个字符串-
ln[2] Match the line below. ln [2]匹配以下行。
ln[3] --This is a string-- ln [3]-这是一个字符串-
ln[4] Match this line start from here. ln [4]匹配此行,从这里开始。
ln[5] -This is the end- ln [5]-这是结局-
I want to extract abc in the string above. 我想在上面的字符串中提取abc。
code: 码:
pattern = re.compile('%s(.*?)%s' % ('--This is a string--', '-This is the end-'))
re.findall(pattern, string)
How can I get the line 4 only, not get line 2 to line 4 ? 我怎样才能只获得第4行,而不是获得第2行到第4行?
Thank you very much. 非常感谢你。
Probably, via this: 大概是这样的:
pattern = re.compile('.*(a.*?c)')
re.findall(pattern, string) # yields ["abc"]
>>> re.findall('a[^a]*c', 'aaaaaaaaabc')
['abc']
>>> re.findall('a[^a]*c', 'aaaaaaaaa c')
['a c']
If you want to replace all instances of repeated characters you could use id or named groups. 如果要替换所有重复字符的实例,则可以使用id或命名组。
Example: 例:
with id: ID:
>>> re.sub('(.)(\\1)+', '\\1', 'abcAAAAabcBBBBabcCCCCabc')
'abcAabcBabcCabc'
with name: 名称:
>>> re.sub('(?P<n>.)(?P=n)+', '\\1', 'abcAAAAabcBBBBabcCCCCabc')
'abcAabcBabcCabc'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.