in reply to Reg exp questions
I'd like to clarify: You want to replace all space characters except for newline with underscore. You want to replace ( or ) with underscore. And you want to replace newline with space (0x20). Is that correct?
$str =~ s/[\h()]/_/g; $str =~ s/\n/ /g;
One issue you are probably having right now is that \s includes \n, so your newlines are being transliterated to _ (underscore) before the second substitution operator is invoked. By the time you call the second one, there are no more newlines to transliterate. The \h metacharacter class includes horizontal whitespace, but not vertical (ie, not \n). \h is mentioned in perluniprops.
Another problem is that \s on the righthand side of a s/// operator is just a plain old string with an escaped s. The \ gets dropped by the "quote-like-operator" interpolation, so even if \n had matched, it would have left you with s instead of space.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reg exp questions
by carolw (Sexton) on Nov 09, 2014 at 12:36 UTC | |
by davido (Cardinal) on Nov 09, 2014 at 17:32 UTC | |
by poj (Abbot) on Nov 09, 2014 at 12:55 UTC |