Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all tokens with non-word characters in Perl?

Tags:

regex

perl

pcre

I am trying to come up with a regex for removing all words that contain non-word characters.

So if it contains a colon, comma, number, bracket etc then remove it from the line, not just the character but the word. I have this so far.

$wordline = s/\s.*\W.*?\s//g;

Does not have to be perfect so removing strings with dash and apostrophe is ok.

like image 938
Brian G Avatar asked Nov 20 '25 00:11

Brian G


1 Answers

$wordline = join(" ", grep(/^\w+$/, split(/\s+/, $wordline)));
like image 197
Andru Luvisi Avatar answered Nov 22 '25 15:11

Andru Luvisi