Regular Expressions Help!

Discussion in 'Perl Misc' started by Leo Shumacher, May 19, 2004.

  1. 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!
    Leo Shumacher, May 19, 2004
    #1
    1. Advertising

  2. Leo Shumacher () 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.
    Malcolm Dew-Jones, May 19, 2004
    #2
    1. Advertising

  3. 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
    John W. Krahn, May 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris R. Timmons

    Re: Help with regular expressions.

    Chris R. Timmons, Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    Chris R. Timmons
    Jul 4, 2003
  2. David Waz...

    Re: Help with regular expressions.

    David Waz..., Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    David Waz...
    Jul 4, 2003
  3. Stephajn Craig

    Regular Expressions....HELP!

    Stephajn Craig, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    3,032
    Cowboy \(Gregory A. Beamer\)
    Jul 16, 2003
  4. Jay Douglas
    Replies:
    0
    Views:
    594
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    222
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page