【发布时间】:2013-06-04 11:19:33
【问题描述】:
如何使用 Python 3 正则表达式模块获取以下字符串的 123 部分?
....XX (a lot of HTML characters)123
这里的... 部分表示由 HTML 字符、单词和数字组成的长字符串。
数字123 是XX 的一个特征。因此,如果有人可以提出一种通用方法,其中XX 可以是AA 或AB 等任何字母,那会更有帮助。
旁注:
我想通过首先识别字符串中的XX,然后识别出现在XX 之后的第一个数字来使用Perl 的\G 运算符。但似乎\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'怎么样? -
可以指定起始位置。匹配=重新搜索(等级,来源); match = re.compile(r'\d+').search(source, match.end());打印(match.group())
-
编译正则表达式的搜索方法接受可选的pos参数。 docs.python.org/2/library/re.html#re.RegexObject.search
标签: python regex string parsing python-3.x