简体   繁体   English

简单的正则表达式删除_和0

[英]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: 我的尝试是: /[^_0]/但由于某种原因它不起作用:

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

Why bother with regex? 为什么要麻烦正则表达式?
It's a simple str_replace that is needed. 这是一个简单的str_replace。

$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 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 "". 您只需要搜索/[_0]/并替换为“”即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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