我有一个这样的字符串:
某地址23号邻里城市
现在,我的目标是将其分为两部分:
某个地址编号23
邻里城市
我猜这需要split
或preg_replace
命令的组合,使用类似/[^0-9]/
之类的命令。无论我如何尝试,我都没有得到理想的结果。
编辑:有2个绝对正确答案,一个使用preg_replace,另一个使用preg_split,祝你好运!
答案 0 :(得分:3)
$example = preg_split('/(?<=\d\D)/', 'some address number 23 neighborhood city', 2);
var_dump($example);
array(2) {
[0]=> string(20) "some address number 23 "
[1]=> string(20) "neighborhood city" }
答案 1 :(得分:3)
您可以将preg_split()与标记(PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
)
<?php
$word= "some address number 23 neighborhood city";
$keywords = preg_split("/(.+[0-9]+)(.+)/", $word,-1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($keywords);
输出: - https://eval.in/1006421
说明: -
/(.+[0-9]+)(.+)/
1st Capturing Group (.+[0-9]+)
.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Capturing Group (.+)
.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
答案 2 :(得分:2)
您可以使用preg_match和capture groups来正确匹配字符串。
<?php
$string = "some address number 23 neighborhood city";
preg_match("/([^\d]*\d*)(.*)$/", $string, $matches);
var_dump($matches);
array(3) {
[0]=>
string(40) "some address number 23 neighborhood city"
[1]=>
string(20) "some address number 23"
[2]=>
string(18) " neighborhood city"
}
修改强>
在正则表达式中,我们希望以尽可能低的步骤实现我们想要的目标。
这是我们的正则表达式:https://regex101.com/r/rihbWM/2我们可以看到它需要 9 步骤,这有点好。
第一捕获组([^ \ d] \ d )
匹配下面列表中不存在的单个字符[^ \ d] * 这在性能方面优于。* \ d因为。* \ d匹配整个字符串 - 然后必须跳回到十进制,性能更差。
- 量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
\ d匹配一个数字(等于[0-9])
\ d *匹配一个数字(等于[0-9])
- 量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
第二捕获组(。*)
。*匹配任何字符(行终止符除外)
- 量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
$断言字符串末尾的位置,或者在字符串末尾的行终止符之前(如果有的话)