postgresql 正则表达式:删除非字母数字,但有一个例外

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15952515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:52:16  来源:igfitidea点击:

Regular expressions: remove non alpha numerics, with an exception

regexpostgresql

提问by David

To remove all non-alpha numeric chars, the regex would be

要删除所有非字母数字字符,正则表达式将是

x = regexp_replace(somestring, '[^a-zA-Z0-9]+', '', 'g')

But what if I want to leave underscores untouched ?

但是如果我想保留下划线不变怎么办?

回答by Oussama

Then you need to use :

然后你需要使用:

x = regexp_replace(somestring, '\W+', '', 'g')

\Wis the same as [^a-zA-Z0-9_]

\W是相同的 [^a-zA-Z0-9_]

回答by James Kyburz

How about using '\W+' that replaces all non a-z and 0-9 leaving _ alone

使用 '\W+' 替换所有非 az 和 0-9 留下 _ 怎么样

So

所以

x = regexp_replace(somestring, '\W+', '', 'g')