match text followed by number

Discussion in 'Perl Misc' started by nick_keighley_nospam@hotmail.com, Jan 14, 2009.

  1. Guest

    I'm trying to match lines that consist of text with a number at the
    end

    Migration reject cause 4

    and I tried this

    if (m/(.*) (\d*)$/)
    {
    print "\$1 is $1 \$2 is $2\n";
    }

    $1 matched the whole line including the number

    if (m/(.*) (\d+)$/)

    failed to match. Is there a way to get something to match the text
    and the number seperatly?

    --
    Nick keighley
     
    , Jan 14, 2009
    #1
    1. Advertising

  2. wrote:
    > I'm trying to match lines that consist of text with a number at the
    > end
    >
    > Migration reject cause 4
    >
    > and I tried this
    >
    > if (m/(.*) (\d*)$/)


    if (m/(.*) (\d+)$/)

    * matches zero ocurrences (or more)
    + requires at least one ocurrence.


    > {
    > print "\$1 is $1 \$2 is $2\n";
    > }
    >
    > $1 matched the whole line including the number
    >
    > if (m/(.*) (\d+)$/)
    >
    > failed to match. Is there a way to get something to match the text
    > and the number seperatly?
    >



    --
    RGB
     
    RedGrittyBrick, Jan 14, 2009
    #2
    1. Advertising

  3. Guest

    On 14 Jan, 12:17, RedGrittyBrick <>
    wrote:
    > wrote:
    > > I'm trying to match lines that consist of text with a number at the
    > > end

    >
    > > Migration reject cause 4

    >
    > > and I tried this

    >
    > > if (m/(.*) (\d*)$/)

    >
    >    if (m/(.*) (\d+)$/)
    >
    > * matches zero ocurrences (or more)
    > + requires at least one ocurrence.
    >
    > > {
    > >      print "\$1 is $1  \$2 is $2\n";
    > > }

    >
    > > $1 matched the whole line including the number

    >
    > > if (m/(.*) (\d+)$/)

    >
    > > failed to match. Is there a way to get something to match the text
    > > and the number seperatly?


    and so...
     
    , Jan 14, 2009
    #3
  4. <> wrote:
    > I'm trying to match lines that consist of text with a number at the
    > end
    >
    > Migration reject cause 4



    Are you absolutely sure that that is what is contained in $_?

    Did you print it out, along with some delimiters to make sure?

    I expect your problem is related to the string being matched against
    rather than to the pattern being used...


    > and I tried this
    >
    > if (m/(.*) (\d*)$/)
    > {
    > print "\$1 is $1 \$2 is $2\n";
    > }
    >
    > $1 matched the whole line including the number



    Not when I tried it:

    -----------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $_ = 'Migration reject cause 4';
    if (m/(.*) (\d*)$/)
    {
    print "\$1 is '$1' \$2 is '$2'\n";
    }
    -----------------

    $1 is 'Migration reject cause' $2 is '4'

    if
    $_ = "Migration reject cause 4 \n";
    then I get the results you get...



    Note that your pattern will also match with

    $_ = ' ';
    or
    $_ = ' 99';

    m/(.+) (\d+)$/ is probably better.


    > Is there a way to get something to match the text
    > and the number seperatly?



    Yes, and you already have that way.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #4
  5. wrote:
    > I'm trying to match lines that consist of text with a number at the
    > end
    >
    > Migration reject cause 4
    >
    > and I tried this
    >
    > if (m/(.*) (\d*)$/)
    > {
    > print "\$1 is $1 \$2 is $2\n";
    > }
    >
    > $1 matched the whole line including the number
    >
    > if (m/(.*) (\d+)$/)
    >
    > failed to match. Is there a way to get something to match the text
    > and the number seperatly?


    Since the first pattern matched, you probably have a blank at the end of
    the line: the "(.*)" greedily matches the entire line apart from the
    trailing blank, which is matched by the blank and the (\d*) matches the
    zero-length digit string at the end of the line.
    The second pattern doesn't match, as the non-zero-length digit-string is
    not at the end of the line.

    Change the pattern to
    /(.*) (\d+)\s*$/

    HTH,

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Jan 14, 2009
    #5
  6. Guest

    On 14 Jan, 12:52, Tad J McClellan <> wrote:
    > <> wrote:


    thanks for addressing my problem!


    > > I'm trying to match lines that consist of text with a number at the
    > > end

    >
    > > Migration reject cause 4

    >
    > Are you absolutely sure that that is what is contained in $_?
    >
    > Did you print it out, along with some delimiters to make sure?


    "Migration reject cause 4
    "

    there's a trailing \n, there appear to be no other trailing characters


    > I expect your problem is related to the string being matched against
    > rather than to the pattern being used...
    >
    > > and I tried this

    >
    > > if (m/(.*) (\d*)$/)
    > > {
    > > print "\$1 is $1 \$2 is $2\n";
    > > }

    >
    > > $1 matched the whole line including the number

    >
    > Not when I tried it:
    >
    > -----------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > $_ = 'Migration reject cause 4';
    > if (m/(.*) (\d*)$/)
    > {
    > print "\$1 is '$1' \$2 is '$2'\n";}
    >
    > -----------------
    >
    > $1 is 'Migration reject cause' $2 is '4'
    >
    > if
    > $_ = "Migration reject cause 4 \n";
    > then I get the results you get...
    >
    > Note that your pattern will also match with
    >
    > $_ = ' ';
    > or
    > $_ = ' 99';
    >
    > m/(.+) (\d+)$/ is probably better.
    >
    > > Is there a way to get something to match the text
    > > and the number seperatly?

    >
    > Yes, and you already have that way.


    thanks the trailing \n was messing it up. Josef Moellers
    pattern fixed my problem.
     
    , Jan 14, 2009
    #6
  7. <> wrote:
    > On 14 Jan, 12:52, Tad J McClellan <> wrote:
    >> <> wrote:



    >> > Migration reject cause 4

    >>
    >> Are you absolutely sure that that is what is contained in $_?
    >>
    >> Did you print it out, along with some delimiters to make sure?

    >
    > "Migration reject cause 4
    > "
    >
    > there's a trailing \n, there appear to be no other trailing characters



    Then Josef's change will not fix the problem.

    So we still do not know how you ended up matching what you said you matched...


    >> I expect your problem is related to the string being matched against
    >> rather than to the pattern being used...



    I still suspect this.


    > thanks the trailing \n was messing it up.



    No it wasn't.

    $_ = "Migration reject cause 4\n";

    still correctly matches the 2 different parts.


    > Josef Moellers
    > pattern fixed my problem.



    His pattern is definitely better, but it is not possible that
    it fixed your problem, as a trailing newline should have matched
    with your original pattern.

    If you had a trailing _space_ (before the newline), then his pattern
    would fix it, so I'm guessing you had a space then but not now...


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #7
  8. Tim Greer Guest

    wrote:

    > I'm trying to match lines that consist of text with a number at the
    > end
    >
    > Migration reject cause 4
    >
    > and I tried this
    >
    > if (m/(.*) (\d*)$/)
    > {
    > print "\$1 is $1 \$2 is $2\n";
    > }
    >
    > $1 matched the whole line including the number
    >
    > if (m/(.*) (\d+)$/)
    >
    > failed to match. Is there a way to get something to match the text
    > and the number seperatly?
    >
    > --
    > Nick keighley


    Use non greedy matching with a wild card. (.*?) (\d+)$ Are you sure
    the end of the string is a digit, and it's preceded by a white space?
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 14, 2009
    #8
  9. Tim Greer <> wrote:
    > wrote:


    >> if (m/(.*) (\d+)$/)
    >>
    >> failed to match.


    > Use non greedy matching with a wild card. (.*?) (\d+)$



    Than cannot possibly have any effect.

    Greediness does not matter here because this pattern is anchored.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #9
  10. Tim Greer Guest

    Tad J McClellan wrote:

    > Tim Greer <> wrote:
    >> wrote:

    >
    >>> if (m/(.*) (\d+)$/)
    >>>
    >>> failed to match.

    >
    >> Use non greedy matching with a wild card. (.*?) (\d+)$

    >
    >
    > Than cannot possibly have any effect.
    >
    > Greediness does not matter here because this pattern is anchored.
    >
    >


    I had meant in regard to the second example, actually (which used .*
    \d*, and just wasn't paying attention earlier when I posted.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 15, 2009
    #10
  11. Tim Greer Guest

    Tad J McClellan wrote:

    > Tim Greer <> wrote:
    >> Tad J McClellan wrote:
    >>
    >>> Tim Greer <> wrote:
    >>>> wrote:
    >>>
    >>>>> if (m/(.*) (\d+)$/)
    >>>>>
    >>>>> failed to match.
    >>>
    >>>> Use non greedy matching with a wild card. (.*?) (\d+)$
    >>>
    >>>
    >>> Than cannot possibly have any effect.
    >>>
    >>> Greediness does not matter here because this pattern is anchored.
    >>>
    >>>

    >>
    >> I had meant in regard to the second example, actually (which used .*
    >> \d*, and just wasn't paying attention earlier when I posted.

    >
    >
    > But that pattern was anchored too, so greediness still could
    > not have any possible effect.
    >
    >


    I wasn't clear. You're correct. I was indeed wrong. I was meaning
    that I wasn't paying attention earlier, in that I quickly read it and
    saw the mention of the greediness (all of the string in $1), yet I
    didn't pay attention to the regex they posted or think about my reply.
    Pardon, I'm having trouble making sense, it's been a long day...
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 15, 2009
    #11
    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. Brent
    Replies:
    1
    Views:
    1,559
  2. =?Utf-8?B?QW5kcmU=?=

    Stylesheets are not followed after migration

    =?Utf-8?B?QW5kcmU=?=, Mar 30, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    385
    Gerry Hickman
    Apr 13, 2006
  3. Replies:
    1
    Views:
    368
    Roedy Green
    Aug 12, 2005
  4. Fahad
    Replies:
    0
    Views:
    1,474
    Fahad
    Nov 16, 2005
  5. Tom
    Replies:
    6
    Views:
    133
Loading...

Share This Page