Regular Expression Query

Discussion in 'Perl' started by lonelyplanet999, Aug 14, 2003.

  1. Hi,

    I'm studying perl programming now. Previously I encountered a script
    with 3 statements as below.

    # space between a +, - or " char b4 a keyword? remove it!
    $in{keywords}=~s/ \+ +/ \+/g;
    $in{keywords}=~s/ \- +/ \-/g;
    $in{keywords}=~s/ \" +/ \"/g;

    I would like to ask how the regexps function as the comment describe ?

    lonelyplanet999, Aug 14, 2003
    1. Advertisements

  2. Guest

    (lonelyplanet999) wrote in message news:<>...

    > Subject: Regular Expression Query

    Poor subject line.


    > # space between a +, - or " char b4 a keyword? remove it!
    > $in{keywords}=~s/ \+ +/ \+/g;
    > $in{keywords}=~s/ \- +/ \-/g;
    > $in{keywords}=~s/ \" +/ \"/g;
    > I would like to ask how the regexps function as the comment describe ?

    They do not. The contstraint "before a keyword" is not in there.

    Lets just look at one:

    s/ \- +/ \-/g;

    The backslashes are redundant, hyphen is not meta in string or regex.

    s/search/relpace/g means replace as many non-overlapping matches for
    the pattern /search/ with the string replace.

    The pattern / - +/ matches a space, followed by a hyphen followed by
    one or more spaces.

    The string " -" is, err..., the string " -".

    BTW the =~ binding operator is used to specify the variable on which
    s/// is to act.

    That's all there is to it.

    No capturing, no interpolation, no subtlties. This really is about
    the simplest s/// example you could imagine.

    If you are at the stage where you have many questions at this level
    then you probably are not at the stage where asking questions in
    newsgroups is an effective use of your time or other people's. You
    should probably be working through some basic Perl tutorials.

    Actually, assuming this is someone else's code, it would appear that
    the person who wrote it is not much further advanced. Anyone who had
    even basic competance in Perl would usually combine these into a
    single s///g

    s/( [-+"]) +/$1/g;

    As I said before attempting to learn Perl programming from this code
    is not a good idea.

    I have already pointed out to you that this newsgroup does not exist.
    Really, it doesn't (it only appears to exist on misconfigured news
    servers). For further explaination please see the FAQ. Please do not
    start any more threads here.
    , Aug 15, 2003
    1. Advertisements

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. VSK
  2. Sriv Chakravarthy
    Sriv Chakravarthy
    Sep 18, 2003
  3. Martin Biddiscombe

    Regular expression query

    Martin Biddiscombe, Feb 3, 2006, in forum: Python
    bruno at modulix
    Feb 6, 2006
  4. Replies:
  5. George

    Regular Expression Query

    George, Jan 24, 2005, in forum: Perl Misc
    Gunnar Hjalmarsson
    Jan 24, 2005

Share This Page