如果特定的正则表达式匹配,修改字符串。

时间:2022-06-02 00:20:14

Working in PHP, I have the following mock string.

在PHP中,我有以下模拟字符串。

"width 40cm height 70cm"

If the string does not contain a colon and does have a space followed by a number, I want to add a colon before that space.

如果字符串不包含冒号,并且有一个空格后面跟着一个数字,我想在这个空格之前添加一个冒号。

The end result I'm looking for is:

我寻找的最终结果是:

"width: 40cm height: 70cm"

I tried a bunch of ways with regex and splitting the string at the match to add the colon, but it was removing the match string. Here is my attempt.

我用regex尝试了很多方法,并在匹配处分割字符串以添加冒号,但它删除了匹配字符串。这是我的尝试。

    if(strpos($s, ':') === false) {
        $array = preg_split('/([0-9])/', $s, -1, PREG_SPLIT_NO_EMPTY);
        $array[0] = trim($array[0]) . ':';
        $s = implode('', $array);
    }

4 个解决方案

#1


1  

I think this will work

我想这行得通

(\w+)(?=\s+\d)
     <------->
     Lookahead to match
     space followed by 
     digits

Regex Demo

Regex演示

PHP Code

PHP代码

$re = "/(\\w+)(?=\\s+\\d)/m"; 
$str = "width 40cm height 70cm\nwidth: 40cm height 70cm"; 

$result = preg_replace($re, "$1:", $str);
print_r($result);

Ideone Demo

Ideone演示

#2


1  

The following regex might work for you:

以下regex可能适合您:

/(?<!:)(?= \d)/g

With replacement: :

替换::

$output = preg_replace('/(?<!:)(?= \d)/', ':', $input);

It matches a position that is followed before space and digit (?= \d) and not preceded by a colon (?<!:). That's why the replacement group can be a colon only.

它匹配空格和数字(?= \d),前面不加冒号(?

This is called lookarounds. Here be both use a positive lookahead (?=...) and a negative lookbehind: (?<!...).

这就是所谓的看看。这里,你可以使用积极的展望(?=…)和消极的展望(?

https://www.regex101.com/r/oH7cI3/1

https://www.regex101.com/r/oH7cI3/1

#3


1  

Use lookarounds:

使用看看:

(?<=[a-z]) # a-z behind
\          # a space
(?=\d)     # a digit ahead

See a demo on regex101.com and replace the occurences with a colon.

请参阅regex101.com上的演示,并使用冒号替换出现的情况。

#4


0  

$string="width: 40cm height: 70cm";

$temp=exploade(" ",$string);

$ temp = exploade(" ",$ string);

foreach($temp as $value){

foreach(临时美元美元值){

if(strstr($value,'width') && strstr($value,'height')){
    $new_temp[]=$value.":";
}else{
    $new_temp[]=$value;
}

} $new_string=implode(" ", $new_temp);

} $ new_string =内爆(",$ new_temp);

#1


1  

I think this will work

我想这行得通

(\w+)(?=\s+\d)
     <------->
     Lookahead to match
     space followed by 
     digits

Regex Demo

Regex演示

PHP Code

PHP代码

$re = "/(\\w+)(?=\\s+\\d)/m"; 
$str = "width 40cm height 70cm\nwidth: 40cm height 70cm"; 

$result = preg_replace($re, "$1:", $str);
print_r($result);

Ideone Demo

Ideone演示

#2


1  

The following regex might work for you:

以下regex可能适合您:

/(?<!:)(?= \d)/g

With replacement: :

替换::

$output = preg_replace('/(?<!:)(?= \d)/', ':', $input);

It matches a position that is followed before space and digit (?= \d) and not preceded by a colon (?<!:). That's why the replacement group can be a colon only.

它匹配空格和数字(?= \d),前面不加冒号(?

This is called lookarounds. Here be both use a positive lookahead (?=...) and a negative lookbehind: (?<!...).

这就是所谓的看看。这里,你可以使用积极的展望(?=…)和消极的展望(?

https://www.regex101.com/r/oH7cI3/1

https://www.regex101.com/r/oH7cI3/1

#3


1  

Use lookarounds:

使用看看:

(?<=[a-z]) # a-z behind
\          # a space
(?=\d)     # a digit ahead

See a demo on regex101.com and replace the occurences with a colon.

请参阅regex101.com上的演示,并使用冒号替换出现的情况。

#4


0  

$string="width: 40cm height: 70cm";

$temp=exploade(" ",$string);

$ temp = exploade(" ",$ string);

foreach($temp as $value){

foreach(临时美元美元值){

if(strstr($value,'width') && strstr($value,'height')){
    $new_temp[]=$value.":";
}else{
    $new_temp[]=$value;
}

} $new_string=implode(" ", $new_temp);

} $ new_string =内爆(",$ new_temp);