Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Regular Expressions Help! (http://www.velocityreviews.com/forums/t886506-regular-expressions-help.html)

Leo Shumacher 05-19-2004 05:46 PM

Regular Expressions Help!
 
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!


Malcolm Dew-Jones 05-19-2004 09:28 PM

Re: Regular Expressions Help!
 
Leo Shumacher (lps@ucla.edu) 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.)



--

(Paying) telecommute programming projects wanted. Simply reply to this.


John W. Krahn 05-19-2004 11:43 PM

Re: Regular Expressions Help!
 
Leo Shumacher wrote:
>
> 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
--
use Perl;
program
fulfillment



All times are GMT. The time now is 04:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.