Python 3 正则表达式最后一次匹配

3

如何使用Python 3的正则表达式模块来提取以下字符串中的123部分?

....XX (a lot of HTML characters)123

这里的...部分表示由HTML字符、单词和数字组成的长字符串。

数字123XX的特征。因此,如果有人能够建议一种通用方法,其中XX可以是任何字母,如AAAB,那就更有帮助了。

附注:
我考虑使用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())

2
你能展示一下你的尝试吗?这样问题会更加清晰。 - jamylak
1
“抓取”它是什么意思?只需使用if '123' in text: print '123'怎么样? - John Zwinck
1
https://dev59.com/wHE85IYBdhLWcg3wYicB - falsetru
2
您可以指定起始位置。match = re.search(grade,source); match = re.compile(r'\d+').search(source, match.end()); print(match.group()) - falsetru
1
编译后的正则表达式搜索方法接受可选的pos参数。http://docs.python.org/2/library/re.html#re.RegexObject.search - falsetru
显示剩余7条评论
1个回答

1

描述

这个正则表达式将匹配字符串值XX,可以用用户输入替换。该正则表达式还要求XX字符串被空格包围或位于示例文本的开头,以防止XX被误认为是单词中的一部分,比如EXXON

(?<=\s|^)\b(xx)\b\s.*?\s\b(\d+)\b(?=\s|$)

enter image description here

代码示例:

我不太熟悉Python,无法提供一个合适的Python示例,因此我包含了一个PHP示例,只是为了展示正则表达式如何工作和捕获组。

<?php
$sourcestring="EXXON abcd XX blah blah 123 more blah blah";
preg_match('/(?<=\s|^)\b(xx)\b\s.*?\s\b(\d+)\b(?=\s|$)/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
 
$matches Array:
(
    [0] => XX blah blah 123
    [1] => XX
    [2] => 123
)

如果您需要实际的字符串位置,在PHP中将如下所示:
$position = strpos($sourcestring, $matches[0]) 

1
只是好奇,你用什么生成图像的? - korylprince
1
@ Korylprince,我正在使用debuggex.com。虽然它不支持向后查找或原子组,但仍然有助于理解表达式的流程。还有regexper.com。他们也做得很好,但它不是实时的,因为你在输入时需要手动刷新。 - Ro Yo Mi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接