Help: Match Error

Discussion in 'Perl Misc' started by Amy Lee, Sep 30, 2008.

  1. Amy Lee

    Amy Lee Guest

    Hello,

    Here's my codes to match the following strings:

    while (<>)
    {
    chomp;
    if (/\d+\s+dG = (\d+)\s+(.*)/)
    {
    print "$1\n";
    print "$2\n";
    }
    }

    And the string is like this.
    97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop

    However, when I run my script it outputs nothing.

    Could you tell me what's going on?

    Thank you very much.

    Best Regards,

    Amy
    Amy Lee, Sep 30, 2008
    #1
    1. Advertising

  2. Amy Lee <> wrote:
    >Hello,
    >
    >Here's my codes to match the following strings:
    >
    >while (<>)
    >{
    > chomp;
    > if (/\d+\s+dG = (\d+)\s+(.*)/)
    > {
    > print "$1\n";
    > print "$2\n";
    > }
    >}
    >
    >And the string is like this.
    >97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop
    >
    >However, when I run my script it outputs nothing.
    >
    >Could you tell me what's going on?


    The minus sign is not a digit.

    jue
    Jürgen Exner, Sep 30, 2008
    #2
    1. Advertising

  3. Amy Lee

    Amy Lee Guest

    On Mon, 29 Sep 2008 19:32:31 -0700, Jürgen Exner wrote:

    > Amy Lee <> wrote:
    >>Hello,
    >>
    >>Here's my codes to match the following strings:
    >>
    >>while (<>)
    >>{
    >> chomp;
    >> if (/\d+\s+dG = (\d+)\s+(.*)/)
    >> {
    >> print "$1\n";
    >> print "$2\n";
    >> }
    >>}
    >>
    >>And the string is like this.
    >>97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop
    >>
    >>However, when I run my script it outputs nothing.
    >>
    >>Could you tell me what's going on?

    >
    > The minus sign is not a digit.
    >
    > jue

    Thanks. And I have noticed that, however if I want to match
    dG = 30 and dG = -30, what RE should I take?
    Use /dg =( | -)(\d+)/ ?

    Amy
    Amy Lee, Sep 30, 2008
    #3
  4. Amy Lee wrote:
    > Hello,
    >
    > Here's my codes to match the following strings:
    >
    > while (<>)
    > {
    > chomp;
    > if (/\d+\s+dG = (\d+)\s+(.*)/)
    > {
    > print "$1\n";
    > print "$2\n";
    > }
    > }
    >
    > And the string is like this.
    > 97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop
    >
    > However, when I run my script it outputs nothing.
    >
    > Could you tell me what's going on?


    \d matches numerical digits, it doesn't match either '-' or '.'.

    Try /\d+\s+dG = (-?[\d.]+)\s+(.*)/ instead.




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Sep 30, 2008
    #4
  5. Amy Lee

    Amy Lee Guest

    On Mon, 29 Sep 2008 19:51:34 -0700, John W. Krahn wrote:

    > Amy Lee wrote:
    >> Hello,
    >>
    >> Here's my codes to match the following strings:
    >>
    >> while (<>)
    >> {
    >> chomp;
    >> if (/\d+\s+dG = (\d+)\s+(.*)/)
    >> {
    >> print "$1\n";
    >> print "$2\n";
    >> }
    >> }
    >>
    >> And the string is like this.
    >> 97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop
    >>
    >> However, when I run my script it outputs nothing.
    >>
    >> Could you tell me what's going on?

    >
    > \d matches numerical digits, it doesn't match either '-' or '.'.
    >
    > Try /\d+\s+dG = (-?[\d.]+)\s+(.*)/ instead.
    >
    >
    >
    >
    > John

    Thank you very much. You show me the way.

    Amy
    Amy Lee, Sep 30, 2008
    #5
  6. On Tue, 30 Sep 2008 09:39:18 +0800,
    Amy Lee <> wrote:
    > Hello,
    >
    > Here's my codes to match the following strings:
    >
    > if (/\d+\s+dG = (\d+)\s+(.*)/)


    You're looking for

    one or more digits,
    one or more whitespace characters,
    "dG = ",
    one or more digits ...

    > And the string is like this.
    > 97 dG = -40.9 cel-mir-41 MI0000012 Caenorhabditis elegans miR-41 stem-loop


    You have here

    one or more digits (97)
    one or more whitespace characters
    "dG = "
    a minus....

    Ah. That must be why it didn't match. A minus is not a digit. Neither,
    BTW is the . in -40.9, so even if you fixed the minus problem, you would
    still not get the whole number that you're interested in, I presume. If
    all your numbers follow the same format, with optional - only, you could
    try

    /\d+\s+dG = (-?\d+\.\d+)\s+(.*)/

    If there are possible + signs, and if the fractional part is also
    optional maybe this will work:

    /\d+\s+dG = ([+-]?\d+(?:\.\d+)?)\s+(.*)/

    How generic you need to get depends on the format of that number in your
    data, and I don't know that. I only have the one example.

    Have a look at the entry in perlfaq4 with the title "How do I determine
    whether a scalar is a number/whole/integer/float?"

    # perldoc -q "determine.*number"

    That entry has many regular expressions, one of which you probably can
    use, if the sufggestions above aren't sufficient.

    Martien
    --
    |
    Martien Verbruggen | We are born naked, wet and hungry. Then
    | things get worse.
    |
    Martien Verbruggen, Sep 30, 2008
    #6
  7. Amy Lee

    Ben Morrow Guest

    Quoth Amy Lee <>:
    > On Mon, 29 Sep 2008 19:32:31 -0700, Jürgen Exner wrote:
    > >
    > > The minus sign is not a digit.
    > >

    > Thanks. And I have noticed that, however if I want to match
    > dG = 30 and dG = -30, what RE should I take?
    > Use /dg =( | -)(\d+)/ ?


    That will work. It would be better to use non-capturing brackets:

    /dg =(?: | -)(\d+)/

    and you don't need the space inside the alternation:

    /dg = (?:|-)(\d+)/

    and you can just use ? instead:

    /dg = -?(\d+)/

    Ben

    --
    All persons, living or dead, are entirely coincidental.
    Kurt Vonnegut
    Ben Morrow, Sep 30, 2008
    #7
  8. Amy Lee <> wrote:

    [ snip how to match a real number ]

    > Thank you very much. You show me the way.



    You needn't be so dependent on others to show you the way.

    You will become an efficient Perl programmer if you learn how
    to discover "ways" for yourself.

    If you have a question about "numbers", you should check the
    Frequently Asked Questions that mention them:

    perldoc -q number

    How do I determine whether a scalar is a number/whole/integer/float?

    which has 7 patterns for matching various types of numbers.

    Then you would have noticed that many of the patterns have "-" character
    in them, and that your pattern did not...

    .... problem solved without asking hundreds of other people to do it for you.

    Be nice to us.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Sep 30, 2008
    #8
    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. hiwa
    Replies:
    0
    Views:
    630
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    463
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    155
Loading...

Share This Page