regexp help

Discussion in 'Perl Misc' started by mani, Sep 30, 2003.

  1. mani

    mani Guest

    Hi,

    I have a problem with finding the regexp for this

    Let us say

    $x="# I have been";

    if($x!~/^#/)
    {
    #stuff here
    }

    will make the code not get into the control loop for the current value
    of x because $x starts with #. This is fine but if i give

    $x=" #I have been\n";

    then the if condition would be true. I want to avoid this I want to
    ignore anything that starts with a <space># or # at the start. How do
    i do it

    I tried /s*^#/ which does not give what i want....

    please help me....
     
    mani, Sep 30, 2003
    #1
    1. Advertising

  2. On 30 Sep 2003 05:29:08 -0700,
    mani <> wrote:

    > if($x!~/^#/)


    > I want to
    > ignore anything that starts with a <space># or # at the start.


    > I tried /s*^#/ which does not give what i want....


    /^ ?#/

    Or, if you meant "any whitespace" instead of "a <space>"

    /^\s?#/

    Or, if you meant any number of whitespace

    /^\s*#/

    And, please, don't just copy this, but read the perlre documentation,
    and read about what I just told you. It'll help you solve these sorts of
    problems yourself in the future.

    Martien
    --
    |
    Martien Verbruggen | Blessed are the Fundamentalists, for they
    | shall inhibit the earth.
    |
     
    Martien Verbruggen, Sep 30, 2003
    #2
    1. Advertising

  3. On Tue, 30 Sep 2003 18:11:18 +0530,
    Kasp <> wrote:
    >> then the if condition would be true. I want to avoid this I want to
    >> ignore anything that starts with a <space># or # at the start. How do
    >> i do it
    >>
    >> I tried /s*^#/ which does not give what i want....

    > -----------^...this should be \s.
    > Try this regular expression:
    > /^(\s)*#/


    Why the capturing parentheses?


    Martien
    --
    |
    Martien Verbruggen | Make it idiot proof and someone will make a
    | better idiot.
    |
     
    Martien Verbruggen, Sep 30, 2003
    #3
  4. In article <blbtli$4b7$>, Kasp wrote:
    [cut]
    >> I tried /s*^#/ which does not give what i want....

    > -----------^...this should be \s.
    > Try this regular expression:
    > /^(\s)*#/


    No need to parenthasize the \s.

    --
    Andreas Kähäri
     
    Andreas Kahari, Sep 30, 2003
    #4
  5. mani

    mani Guest

    Thanks a lot for helping me. As i see the solution is /^\s*#/

    I have questions. Martien, I am ok at reading documents but sometimes
    i find it better discussing with some good people like you guys :).
    How does that \s help?. I mean what does it mean?..... Can u tell me?.
    I thought it is

    /^s*#/.. My interpretation is anything that starts with a space or any
    number of space followed by a hash and if it is the beginning and so
    this regexp suffices but you guys have said /^\s*#/ why this \?????


    Martien Verbruggen <> wrote in message news:<>...
    > On 30 Sep 2003 05:29:08 -0700,
    > mani <> wrote:
    >
    > > if($x!~/^#/)

    >
    > > I want to
    > > ignore anything that starts with a <space># or # at the start.

    >
    > > I tried /s*^#/ which does not give what i want....

    >
    > /^ ?#/
    >
    > Or, if you meant "any whitespace" instead of "a <space>"
    >
    > /^\s?#/
    >
    > Or, if you meant any number of whitespace
    >
    > /^\s*#/
    >
    > And, please, don't just copy this, but read the perlre documentation,
    > and read about what I just told you. It'll help you solve these sorts of
    > problems yourself in the future.
    >
    > Martien
     
    mani, Oct 1, 2003
    #5
  6. mani

    Sam Holden Guest

    On 30 Sep 2003 22:04:53 -0700, mani <> wrote:
    > Thanks a lot for helping me. As i see the solution is /^\s*#/
    >
    > I have questions. Martien, I am ok at reading documents but sometimes
    > i find it better discussing with some good people like you guys :).
    > How does that \s help?. I mean what does it mean?..... Can u tell me?.
    > I thought it is
    >
    > /^s*#/.. My interpretation is anything that starts with a space or any
    > number of space followed by a hash and if it is the beginning and so
    > this regexp suffices but you guys have said /^\s*#/ why this \?????


    s matches 's'.
    \s matches whitespace.

    perldoc perlre

    --
    Sam Holden
     
    Sam Holden, Oct 1, 2003
    #6
  7. mani <> wrote:


    > Martien, I am ok at reading documents but sometimes
    > i find it better discussing with some good people like you guys :).



    Reading the applicable docs, then posting if you don't understand is OK.

    Asking us to read the docs for you is not OK.

    (there is one of "you" and thousands of "us". Not an efficient use of time.)


    > How does that \s help?. I mean what does it mean?.....



    The docs for regular expressions say:

    perldoc perlre

    \s Match a whitespace character


    > Can u tell me?.



    Yes. It will match a whitespace character.


    > I thought it is



    Guessing at a language's features is no way to go about
    software development.


    > /^s*#/.. My interpretation is anything that starts with a space



    No, that is anything that starts with the character "s".



    [ snip full-quote.
    Please see the Posting Guidelines that are posted here frequently.
    ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 1, 2003
    #7
    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. Greg Hurrell
    Replies:
    4
    Views:
    163
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    490
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    192
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    181
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page