[英]Python 3 Regex Last Match
如何使用Python 3 regex模块获取以下字符串的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())
此正则表达式将匹配可以用用户输入替换的字符串值XX
。 正则表达式还要求XX
字符串被空格包围或在示例文本的开头处,以防止在像EXXON
这样的单词内找到XX
的意外边缘情况。
(?<=\\s|^)\\b(xx)\\b\\s.*?\\s\\b(\\d+)\\b(?=\\s|$)
我不太了解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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.