Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 尽可能晚地查找正则表达式模式的出现_Regex_String_Expression - Fatal编程技术网

Regex 尽可能晚地查找正则表达式模式的出现

Regex 尽可能晚地查找正则表达式模式的出现,regex,string,expression,Regex,String,Expression,我试图从如下字符串中提取信息:“Hello test 1 23 45 678 901 234 C test test2” 我想提取2345678901234 C 我能想到的最好的正则表达式是(\d\s?{13}C?(C和空格是可选的) 但是,该正则表达式提取的是12345678901233,而不是我想要的模式。 我知道正则表达式从左到右扫描字符串,这解释了这种行为。。。有没有办法提取这些信息 提前谢谢。我建议 \d(?:\s?\d){13,}(?:\s?C)? 看 细节 \d-一个数字 (?

我试图从如下字符串中提取信息:
“Hello test 1 23 45 678 901 234 C test test2”

我想提取
2345678901234 C

我能想到的最好的正则表达式是
(\d\s?{13}C?
(C和空格是可选的)

但是,该正则表达式提取的是
12345678901233
,而不是我想要的模式。 我知道正则表达式从左到右扫描字符串,这解释了这种行为。。。有没有办法提取这些信息

提前谢谢。我建议

\d(?:\s?\d){13,}(?:\s?C)?

细节

  • \d
    -一个数字
  • (?:\s?\d){13,}
    -可选空格和数字出现三次或三次以上
  • (?:\s?C)?
    -可选出现一个可选空格,然后出现一个
    C

您可以选择所需的阈值,例如
{13,14}
或只是
{14}
,如果
C
是强制性的,则需要删除最后一个
(?:
)?
,并使用
\d(?:\s?\d){13,}\s?C

(\d\s?{13,}C>?在
13
之后添加逗号,以匹配13个或更多出现的数字,后跟可选空格。也许,
\d(?:\s?\d){13,}(?:\s?C)
更好。看,这就是我要找的,谢谢!我将使用您的正则表达式,并只保留每个匹配的最后14个字符