[英]Quick regex pattern in PHP
I have a large string chunk of text, and I need to extract all occurrences of text matching the following pattern: 我有一个很大的文本字符串块,我需要提取所有匹配以下模式的文本:
QXXXXX-X
(where X can be any digit, 0-9). QXXXXX-X
(其中X可以是0-9的任何数字)。
How do I do this in PHP? 如何在PHP中做到这一点?
<?php
preg_match_all("","Q05546-8 XXX Q13323-0",$output,PREG_PATTERN_ORDER);
print_r($output);
?>
Here you go: 干得好:
preg_match_all('/\bQ[0-9]{5}-[0-9]\b/',"Q05546-8 XXX Q13323-0",$output,PREG_PATTERN_ORDER);
print_r($output);
Or, you can use shorthand class \\d
for a digit: \\bQ\\d{5}-\\d\\b
. 或者,您可以将速记类
\\d
用于数字: \\bQ\\d{5}-\\d\\b
。
Regex explanation : 正则表达式说明 :
\\b
- Word boundary (we are either at the beginning of between a word character ( [a-zA-Z0-9_]
) and a non-word one (all others) \\b
单词边界(我们位于单词字符( [a-zA-Z0-9_]
)和非单词字符(所有其他字符)之间的开头 Q
- Literal case-sensitive Q
Q
区分大小写的Q
[0-9]{5}
- Exactly 5 (due to {5}
) digits from 0
to 5
range [0-9]{5}
-切合5(由于{5}
)位从0
到5
范围 -
- Literal hyphen -
-连字符 [0-9]
- Exactly 1 digit from 0
to 5
[0-9]
0
到5
1位数字 \\b
- Again a word boundary. \\b
再次是单词边界。 If you have these values inside longer sequences, you may consider using \\bQ[0-9]{5}-[0-9](?![0-9])
or using shorthand classes, \\bQ\\d{5}-\\d(?!\\d)
. 如果您在较长的序列中包含这些值,则可以考虑使用
\\bQ[0-9]{5}-[0-9](?![0-9])
或速记类\\bQ\\d{5}-\\d(?!\\d)
Array
(
[0] => Array
(
[0] => Q05546-8
[1] => Q13323-0
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.