need help with postgresql regexp_replace
I need to get rid of trailing repeating non_alphanumeric symbols like
"thanks ! !!!!!!!"
to "thanks !"
If these different symbols then they are ignored.
I quite new to r开发者_如何学运维egex, so I came up with
regexp_replace('Thanks . . . . ', '((\\W)\\s*)(\\2\\s*)+', '\\2')
to try it out.
However i realise the trailing space after 'thanks' causes some problem. I would get back "thanks "
instead of "thanks ."
This is kinda strange because I used an online regex tool and the first whitespace was not matched. Can anyone help?
note: I did insert the double backslash.
Replace
(\W)(\s*\1)+
with
\1
I don't know PostgreSQL, but from your example, I'm guessing:
regexp_replace('Thanks . . . . ', '(\\W)(\\s*\\1)+', '\\1')
This will also replace leading multiple spaces with a single space. If you don't want that (i. e. if you want leading spaces to be left alone entirely), then use
([^\s\w])(\s*\1)+ // '([^\\s\\w])(\\s*\\1)+'
instead.
try like this:
select regexp_replace('Thanks ! !!!!!!!!', '(\\s*)((\\W)\\s*)(\\2\\s*)+', '\\1\\2');
result:
Thanks !
精彩评论