Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl regex replace non word characters except for ::

Tags:

regex

perl

I would like to have some perl regex to replace any non-word characters like so:

s/\W//g;

However, if there are two colons following eachother like ::, I would not like to replace those. Does anyone know how to accomplish this? Thanks!

like image 809
srchulo Avatar asked Nov 29 '25 16:11

srchulo


1 Answers

/\W/ is /[^\w]/, so /[^\w:]/ would delete every non-word char except colons.

You also want to delete lone colons (/(?<!:):(?!:)/), so the final solution is

s/[^\w:]|(?<!:):(?!:)//g;
like image 101
ikegami Avatar answered Dec 02 '25 06:12

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!