我遇到了一个我无法解决的问题。我有一个由不需要的文本和10位数字组成的字符串,总是以“2”或“6”开头。我需要将10位数的数字放入数组中。我想到正则表达式,发现这篇文章Regular Expression for matching a numeric sequence?非常接近我需要的东西(除了降序/升序的东西),因为我永远无法理解正则表达式,我无法修改我的需求。如果有人能帮到我,我会非常感激!
以下是我的字符串示例:
".........693 7098469 - ZQH X Bop. Hrtepou 50 flerpoUrroXn ........210 5014166 - 0E000PA E KapaoAn Anpn-rPou 21
EAArivtg .....................................................210 9618677 - MAPIA KapaoAri Arpn-rptou 21 Elanvolo .. 210 9643623 - MAPIA E ...................................................... 210 9643887 - MAPIA 0 loucrrivou 8 HX.toOrran ..............210 9914534 AIPITAKHE APTEMIOE n Avrtnopou 22
Reptcrrept ....._.........._......._................697 7440896 , -10AN."
非常感谢您提前! 来自希腊的问候!
答案 0 :(得分:4)
当我看到你的字符串时,你的数字之间有一个空格,如果你想严格做出选择,这就是正则表达式:
[62]\d{2}\s*\d{7}
的说明强> :
[62] # Start with 6 or 2
\d{2} # 2 more digits
\s* # any number of white spaces
\d{7} # 7 more digits
和PHP代码,preg_match_all
匹配所有出现的字符串:
preg_match_all("/[62]\d{2}\s*\d{7}/", $text, $matches);
<强>输出强>:
Array
(
[0] => 693 7098469
[1] => 210 5014166
[2] => 210 9618677
[3] => 210 9643623
[4] => 210 9643887
[5] => 210 9914534
[6] => 697 7440896
)
答案 1 :(得分:3)
也许是这样的:
<?php
$x=
".........693 7098469 - ZQH X Bop. Hrtepou 50 flerpoUrroXn ........210 5014166 - 0E000PA E KapaoAn Anpn-rPou 21 EAArivtg ....................................................210 9618677 - MAPIA KapaoAri Arpn-rptou 21 Elanvolo .. 210 9643623 - MAPIA E ...................................................... 210 9643887 - MAPIA 0 loucrrivou 8 HX.toOrran ..............210 9914534 AIPITAKHE APTEMIOE n Avrtnopou 22
Reptcrrept ....._.........._......._................697 7440896 , -10AN.";
$x=str_replace(' ','',$x);
preg_match_all('/((2|6)\d{9})/',$x,$matches);
print_r($matches[0]);
结果:
Array
(
[0] => 6937098469
[1] => 2105014166
[2] => 2109618677
[3] => 2109643623
[4] => 2109643887
[5] => 2109914534
[6] => 6977440896
)
答案 2 :(得分:2)