Regex仅由最后一个空格字符分隔一个字符串。

时间:2022-08-22 11:54:41

hopefully this should be a quick and simple one, using PHP I'm trying to split a string into an array but by only the last instance of whitespace. So far I have...

希望这是一个快速而简单的方法,使用PHP,我试图将一个字符串分割成一个数组,但只通过最后一个空格实例。到目前为止我有……

$str="hello this is     a    space";
$arr=preg_split("/\s+/",$str);
print_r($arr);

Array ( [0] => hello [1] => this [2] => is [3] => a [4] => space ) 

...which splits by all instances of whitespace.

…由所有空格实例分割。

How can I expand this regular expression to split by only the last instance of whitespace? To become...

如何将这个正则表达式扩展为仅分割空格的最后一个实例?成为……

Array ( [0] => hello this is     a [1] => space ) 

Thank you in advance of your help!

谢谢您的帮助!

3 个解决方案

#1


39  

Try:

试一试:

$arr=preg_split("/\s+(?=\S*+$)/",$str);

Edit

编辑

A short explanation:

一个简短的解释:

The (?= ... ) is called a positive look ahead. For example, a(?=b) will only match a single 'a' if the next character (the one to the right of it) is a 'b'. Note that the 'b' is not a part of the match!

(?=…)被称为积极向前看。例如,如果下一个字符(右边的字符)是一个“b”,那么a(?=b)将只匹配一个“a”。注意“b”不是比赛的一部分!

The \S is just a short-hand for the character class [^\s]. In other words: it matches a single character other than a white space character. The + after the * makes the character class \S possessive.

\ S只是一个速记的字符类^ \[S]。换句话说:它匹配一个单独的字符,而不是空格字符。*后面的+表示字符类的所有格。

Finally, the $ denotes the end of the string.

最后,$表示字符串的末尾。

To recap, the complete regex \s+(?=\S*+$) would read in plain English as follows:

总结一下,完整的regex \s+(?=\ s *+$)将会以简单的英文阅读如下:

match one or more white space characters only when looking ahead of those white space characters zero or more characters other than white space characters, followed by the end of the string, can be seen.

只有在看到空白字符时,才会匹配一个或多个空白字符,即除了空白字符之外的零字符或多个字符,然后是字符串的末尾。

#2


2  

This should work:

这应该工作:

$str="hello this is a  space";

preg_match('~^(.*)\s+([^\s]+)$~', $str, $matches);
$result = array($matches[1], $matches[2]);

You could do it without a regex:

你可以不用正则表达式来做:

$parts = array_map('trim', explode(' ', $str));
$result = array(
    implode(' ', array_slice($parts, 0, -1)),
    end($parts)
);

or

$lastSpace = strrpos($str, ' ');
$str1 = trim(substr($str, 0, $lastSpace));
$str2 = trim(substr($str, $lastSpace));
$result = array( $str1, $str2 );

#3


0  

If the * and + after \S dupicated? Only /\s+(?=\S+$)/ or /\s+(?=\S*$)/ is enough depends on the need.

如果*和+后的\S有错误?只/ \ s +(? = \ s + $)/或/ \ s +(? = \ s * $)/足够取决于所需要的。

#1


39  

Try:

试一试:

$arr=preg_split("/\s+(?=\S*+$)/",$str);

Edit

编辑

A short explanation:

一个简短的解释:

The (?= ... ) is called a positive look ahead. For example, a(?=b) will only match a single 'a' if the next character (the one to the right of it) is a 'b'. Note that the 'b' is not a part of the match!

(?=…)被称为积极向前看。例如,如果下一个字符(右边的字符)是一个“b”,那么a(?=b)将只匹配一个“a”。注意“b”不是比赛的一部分!

The \S is just a short-hand for the character class [^\s]. In other words: it matches a single character other than a white space character. The + after the * makes the character class \S possessive.

\ S只是一个速记的字符类^ \[S]。换句话说:它匹配一个单独的字符,而不是空格字符。*后面的+表示字符类的所有格。

Finally, the $ denotes the end of the string.

最后,$表示字符串的末尾。

To recap, the complete regex \s+(?=\S*+$) would read in plain English as follows:

总结一下,完整的regex \s+(?=\ s *+$)将会以简单的英文阅读如下:

match one or more white space characters only when looking ahead of those white space characters zero or more characters other than white space characters, followed by the end of the string, can be seen.

只有在看到空白字符时,才会匹配一个或多个空白字符,即除了空白字符之外的零字符或多个字符,然后是字符串的末尾。

#2


2  

This should work:

这应该工作:

$str="hello this is a  space";

preg_match('~^(.*)\s+([^\s]+)$~', $str, $matches);
$result = array($matches[1], $matches[2]);

You could do it without a regex:

你可以不用正则表达式来做:

$parts = array_map('trim', explode(' ', $str));
$result = array(
    implode(' ', array_slice($parts, 0, -1)),
    end($parts)
);

or

$lastSpace = strrpos($str, ' ');
$str1 = trim(substr($str, 0, $lastSpace));
$str2 = trim(substr($str, $lastSpace));
$result = array( $str1, $str2 );

#3


0  

If the * and + after \S dupicated? Only /\s+(?=\S+$)/ or /\s+(?=\S*$)/ is enough depends on the need.

如果*和+后的\S有错误?只/ \ s +(? = \ s + $)/或/ \ s +(? = \ s * $)/足够取决于所需要的。