我在使用正则表达式拆分字符串时遇到问题。
我搜索了regex以在大写单词上拆分字符串,但我需要的是分割字符串,如下例所示。
拥有此示例数据:
This is First SentenceThis is Second Sentence
......字符串应该像这样拆分:
This is First Sentence
This is Second Sentence
任何人都知道解决方案吗?
答案 0 :(得分:2)
$str = 'This is First SentenceThis is Second Sentence';
$results = preg_split('~[a-z]\K(?=[A-Z])~', $str);
print_r($results);
或者同时使用look-behind和lookahead断言:
$results = preg_split('~(?<=[a-z])(?=[A-Z])~', $str);
输出
Array
(
[0] => This is First Sentence
[1] => This is Second Sentence
)