match text followed by number

G

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?
 
R

RedGrittyBrick

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.
 
T

Tad J McClellan

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.
 
J

Josef Moellers

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
 
G

Guest

(e-mail address removed) <[email protected]> wrote:

thanks for addressing my problem!

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...




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.


Yes, and you already have that way.

thanks the trailing \n was messing it up. Josef Moellers
pattern fixed my problem.
 
T

Tad J McClellan

"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 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...
 
T

Tim Greer

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?

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?
 
T

Tim Greer

Tad said:
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.
 
T

Tim Greer

Tad said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top