有这么一个字符串
s = 'PN:\nabcd\nPN:\nabcd\nabcd\nPN:\nabcd\nabcd\n'
即
s = '''
PN:
abcd
PN:
abcd
abcd
PN:
abcd
abcd
'''
我只想匹配到其中两个PN
之间的\nabcd\n
或者\nabcd\nabcd\n
,然后我用了如下正则:
result = re.search('P\s*N:\s*(.*[\s\S]*)P.*', s, re.IGNORECASE).group(0)
最后输出result的结果为:
PN:
abcd
PN:
abcd
abcd
PN:
求大神指点~
Well, the problem has been solved, just don’t use regular expressions...
Sure enough, I was misled by myself because I relied too much on regular expressions.
Attached code:
Output result:
Then you only need to write a function and pass in parameters from the outside to get the content between any two PNs...
I don’t understand python, so I wrote js, I don’t know if it will help
Results