Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   match text followed by number (http://www.velocityreviews.com/forums/t909438-match-text-followed-by-number.html)

nick_keighley_nospam@hotmail.com 01-14-2009 12:11 PM

match text followed by number
 
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

RedGrittyBrick 01-14-2009 12:17 PM

Re: match text followed by number
 

nick_keighley_nospam@hotmail.com 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

nick_keighley_nospam@hotmail.com 01-14-2009 12:43 PM

Re: match text followed by number
 
On 14 Jan, 12:17, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
> nick_keighley_nos...@hotmail.com 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...


Tad J McClellan 01-14-2009 12:52 PM

Re: match text followed by number
 
nick_keighley_nospam@hotmail.com <nick_keighley_nospam@hotmail.com> 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/"

Josef Moellers 01-14-2009 01:28 PM

Re: match text followed by number
 
nick_keighley_nospam@hotmail.com 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

nick_keighley_nospam@hotmail.com 01-14-2009 02:06 PM

Re: match text followed by number
 
On 14 Jan, 12:52, Tad J McClellan <ta...@seesig.invalid> wrote:
> nick_keighley_nos...@hotmail.com <nick_keighley_nos...@hotmail.com> 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.



Tad J McClellan 01-14-2009 03:21 PM

Re: match text followed by number
 
nick_keighley_nospam@hotmail.com <nick_keighley_nospam@hotmail.com> wrote:
> On 14 Jan, 12:52, Tad J McClellan <ta...@seesig.invalid> wrote:
>> nick_keighley_nos...@hotmail.com <nick_keighley_nos...@hotmail.com> 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/"

Tim Greer 01-14-2009 05:20 PM

Re: match text followed by number
 
nick_keighley_nospam@hotmail.com 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!

Tad J McClellan 01-14-2009 09:48 PM

Re: match text followed by number
 
Tim Greer <tim@burlyhost.com> wrote:
> nick_keighley_nospam@hotmail.com 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/"

Tim Greer 01-15-2009 12:15 AM

Re: match text followed by number
 
Tad J McClellan wrote:

> Tim Greer <tim@burlyhost.com> wrote:
>> nick_keighley_nospam@hotmail.com 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!


All times are GMT. The time now is 11:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.