Friday, 15 May 2015

regex - Perl - Removing all special characters except a few -



regex - Perl - Removing all special characters except a few -

so came across perl regex "term" allows remove punctuation. here code:

$string =~ s/[[:punct:]]//g;.

however proceeds remove special characters. there way particular regex look can modified example, removes special characters except hyphens. stated on previous question perl, new language, obvious things don't come obvious me. help :_

change code below remove punctuations except hyphen,

$string =~ s/(?!-)[[:punct:]]//g;

demo

use strict; utilize warnings; $string = "foo;\"-bar'.,...*(){}[]----"; $string =~ s/(?!-)[[:punct:]]//g; print "$string\n";

output:

foo-bar----

regex perl

No comments:

Post a Comment