如何使用Python 3的正则表达式模块来提取以下字符串中的123
部分?
....XX (a lot of HTML characters)123
这里的...
部分表示由HTML字符、单词和数字组成的长字符串。
数字123
是XX
的特征。因此,如果有人能够建议一种通用方法,其中XX
可以是任何字母,如AA
或AB
,那就更有帮助了。
附注:
我考虑使用Perl的\G
运算符,首先在字符串中识别XX
,然后识别出在XX
之后出现的第一个数字。但似乎\G
运算符在Python 3中不起作用。
我的代码:
import re
source='abcd XX blah blah 123 more blah blah'
grade=str(input('Which grade?'))
#here the user inputs XX
match=re.search(grade,source)
match=re.search('\G\D+',source)
#Trying to use the \G operator to get the location of last match.Doesn't work.
match=re.search('\G\d+',source)
#Trying to get the next number after XX.
print(match.group())
if '123' in text: print '123'
怎么样? - John Zwinck