简体   繁体   中英

simple regex to remove _ and 0

I'm looking for an regex that converts the following strings (string => result):

_0001 => 1
_0001r => 1r
_0021v-s001r => 21v-s1r
_0000_0001r => 1r

It should essentially remove the _ and all zeros.

My attempt is: /[^_0]/ but for some reason it doesn't work:

https://regex101.com/r/4CWo9S/3

Why bother with regex?
It's a simple str_replace that is needed.

$str = "_0001 => 1
_0001r => 1r
_0021v-s001r => 21v-s1r
_0000_0001r => 1r";

echo str_replace(["0","_"], "", $str);

output:

1 => 1
1r => 1r
21v-s1r => 21v-s1r
1r => 1r

https://3v4l.org/BrL1M

根据您的问题,我假设您的意思是/[_0]/ ^将否定字符类。

It's because you're negating the search with the ^ token. You need just to search for /[_0]/ and replace with "".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM