Regular Expressions Help!

L

Leo Shumacher

Hi,
I have a project due today and am struggling with this line:

s/[,;:!\?\.\"\\\/\]\[]{1,}/ /g;
I am trying to replace the following characters with whitespace (These
are word separators and I'm writing a word-counting program. I am
having trouble figuring out which of those characters need a '\' in
front of them, since perl doesn't give me an error even with -w when I
don't use them in the right places.. Here are the characters in a more
accesible format:

,;:! --> These don't need a '\' in front (I think)
?."\/][ --> These all need it?
@ # $ % ^ & * ( ) + - = ~ ` { } < > | --> These I am not sure about.
Please help!
 
M

Malcolm Dew-Jones

Leo Shumacher ([email protected]) wrote:
: Hi,
: I have a project due today and am struggling with this line:

: s/[,;:!\?\.\"\\\/\]\[]{1,}/ /g;
: I am trying to replace the following characters with whitespace (These
: are word separators and I'm writing a word-counting program. I am
: having trouble figuring out which of those characters need a '\' in
: front of them, since perl doesn't give me an error even with -w when I
: don't use them in the right places.. Here are the characters in a more
: accesible format:

: ,;:! --> These don't need a '\' in front (I think)
: ?."\/][ --> These all need it?
: @ # $ % ^ & * ( ) + - = ~ ` { } < > | --> These I am not sure about.
: Please help!

When it doubt, escape them all (not everyone appears to agree with that,
but I find it works very well). I say that if you need to quote the
characters to be confident you were correct, (and it is a systematic way
to ensure that), then that was means the characters needed to be quoted.

You can also use quotemeta to quote the things that perl thinks may need
quoting, but you have to be sure you are asking it to quote the correct
characters, and this can still end up quoting things that may not need to
be quoted for a specific task.


$chars = <<'EO_CHARS';
@#$%^&*()+-=~`{}<>|?."\/][,;:!
EO_CHARS

# examine the characters to be sure we didn't get it wrong
print $chars;

# now quote them
$quoted = quotemeta($chars);

# lets take a look
print $quoted;

(In the above, there is a trailing \ which is the new line being
escaped, so the above cannot be used as-is in real life.)
 
J

John W. Krahn

Leo said:
I have a project due today and am struggling with this line:

s/[,;:!\?\.\"\\\/\]\[]{1,}/ /g;
I am trying to replace the following characters with whitespace (These
are word separators and I'm writing a word-counting program. I am
having trouble figuring out which of those characters need a '\' in
front of them, since perl doesn't give me an error even with -w when I
don't use them in the right places.. Here are the characters in a more
accesible format:

,;:! --> These don't need a '\' in front (I think)
?."\/][ --> These all need it?
@ # $ % ^ & * ( ) + - = ~ ` { } < > | --> These I am not sure about.
Please help!

Different characters have different meanings in regular expressions and
in character classes and in double quoted strings (and in printf formats
and in pack formats, etc., etc.). Most of the special characters for
these things can be found in the perlop.pod document.

To simplify things, don't use the substitution operator at all:

tr',;:!?."\/][' 's;

The only character you need to escape with that is ' because it is used
as the delimiter.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top