【发布时间】:2013-11-08 23:41:26
【问题描述】:
使用以下通过 UI 生成但显示两次以显示可能变体的字符串;
$string = 'The quick brown fox jumped over the LE300 tractor';
或
$string = 'The quick brown fox jumped over the LE 300 tractor';
或
$string = 'The quick brown fox jumped over the 300 series tractor';
我想要提取 LE300、LE 300 或仅提取 300(假设访客没有进入 LE)
因此,我创建了一个 preg_match() 来提取这些位。
使用以下代码,我可以提取 LE300 或 LE 300,但不仅仅是 300。
preg_match('/(?<=\s|^)[a-zA-Z]{1,3} ?\d\d\d? ?[a-zA-Z]{0,1}? (?=\s|$)/', $string, $matches);
我试过了;
preg_match('/(?<=\s|^)[a-zA-Z]{1,3} ?\d\d\d? ?[a-zA-Z]{0,1}? ?| [0-9]{2,3} (?=\s|$)/', $Title, $matches);
和
preg_match('/(?<=\s|^)[a-zA-Z]{1,3} ?\d\d\d? ?[a-zA-Z]{0,1}? ?| \d\d\d? (?=\s|$)/', $Title, $matches);
但无论我做什么,我都无法提取独立的 2 位或 3 位数字。
如果有人对如何纠正这个问题有任何想法,如果你能与我分享,我将不胜感激
【问题讨论】:
-
为什么不把
{1,3}改成{0,3},这样数字前可以有零个字母?
标签: php regex preg-match