Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with a simple match

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
What is the best way to match 5 consecutive numbers from a $line to create a new variable?

I've already isolated the $line from a <FILE> that has the info I need.

$line = &quot;Member ID: 12345&quot;;

$number = /5_numbers_from_$line/i;
(this needs to be the 12345 part of the above $line, in the same order they are already in.)

Thanks in advance,

Robert Hughes
 
hi robert,

you can try one of these:

$line = &quot;Member ID: 12345&quot;;
$line =~ s/.*:\s(\d+)/\1/g;
print &quot;$line\n&quot;;

OR

$line = &quot;Member ID: 12345&quot;;
($mem, $id) = split/: /, $line;
print &quot;$id\n&quot;;


just a suggestion, cos i'm a still a perl novice.

pearl2
 
You could also use
Code:
$line =~ /(\d+)$/; # grab digits at the end of line
$id = $1;
 
Since a match used in a list context returns all matching parenthesized expressions, you could also simplify to:
Code:
($id) = ($line =~ /(\d{5})/);
The expression on the left must be in parens to force a list context. Note that this match, unlike the one above, also limits to exactly 5 digits, which I gather is what you wanted.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top