I have a string that is something like the example below,
我有一个字符串,类似于下面的例子,
assd *AA*the fox saw the outcome *EAA*saassdsd
I want to remove the whitespace between AA and EAA using regex. Using the code below I can highlight everything between AA and EAA but I only want all the spaces to be highlighted. I believe this (.*) is grouping all the characters. Can someone point me in the right direction thanks?
我想使用正则表达式删除AA和EAA之间的空格。使用下面的代码我可以突出显示AA和EAA之间的所有内容,但我只希望突出显示所有空格。我相信这个(。*)正在对所有角色进行分组。有人能指出我正确的方向谢谢吗?
\*AA\*(.*)\*EAA\*
1 个解决方案
#1
1
From your question, it appears that you want to remove all spaces from given string.
从您的问题,您似乎要删除给定字符串中的所有空格。
function sanitize($string)
{
$re = "/^(.*?AA)(.*)(EAA.*)$/i";
preg_match_all($re, $string, $matches);
if($matches) {
$p1 = $matches[1][0];
$p2 = $matches[2][0];
$p3 = $matches[3][0];
$p2 = preg_replace("/\\s+/", "", $p2);
$result = $p1.$p2.$p3;
return $result;
}
return $string;
}
$str = "assd *AA*the fox saw the outcome *EAA*saassdsd ";
print(sanitize($str))
This will render:
这将呈现:
assd *AA*thefoxsawtheoutcome*EAA*saassdsd
#1
1
From your question, it appears that you want to remove all spaces from given string.
从您的问题,您似乎要删除给定字符串中的所有空格。
function sanitize($string)
{
$re = "/^(.*?AA)(.*)(EAA.*)$/i";
preg_match_all($re, $string, $matches);
if($matches) {
$p1 = $matches[1][0];
$p2 = $matches[2][0];
$p3 = $matches[3][0];
$p2 = preg_replace("/\\s+/", "", $p2);
$result = $p1.$p2.$p3;
return $result;
}
return $string;
}
$str = "assd *AA*the fox saw the outcome *EAA*saassdsd ";
print(sanitize($str))
This will render:
这将呈现:
assd *AA*thefoxsawtheoutcome*EAA*saassdsd