Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Regex and leading zeros in numbers

by Anonymous Monk
on Mar 24, 2004 at 15:57 UTC ( #339440=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have two files and I need to mach this two values:
20055 with 020055
I am trying to use reg.exp. like
if(A variable with 020055) $nums=~s/(\d)(\d)(\d)(\d)(\d)(\d)/(\d)(\d)(\d)(\d)(\d)/g;
I need to extract the first zero from the second number to mach with the first, I am stuck, little help!!!!

20040326 Edit by BazB: Changed title from 'Reg. Exp.'

Replies are listed 'Best First'.
Re: Regex and leading zeros in numbers
by Roy Johnson (Monsignor) on Mar 24, 2004 at 16:23 UTC
    Have you tried numeric comparison? Is that what you want?
    my ($s1, $s2) = qw(20055 020055); print(($s1 == $s2) ? "equal\n" : "not equal\n");
    If you want to strip leading zeroes:

    The PerlMonk tr/// Advocate
Re: Regex and leading zeros in numbers
by Happy-the-monk (Canon) on Mar 24, 2004 at 16:25 UTC

    You did not tell us what the files look like. Anyway:

    • Extract a series of digits: m/(\d+)/
    • Extract a series of digits, throw away leading "0": m/0(\d+)/

    You can compare numbers numerically with the == operator, so you do not need to care about leading zeros.

Re: Regex and leading zeros in numbers
by flyingmoose (Priest) on Mar 24, 2004 at 17:42 UTC
    Regarding the (\d)(\d) repitition you are using, I highly recommend you pick up a copy of "Learning Perl" and "Programming Perl" -- the O'Reilly Camel and Lllama books, as these explain regexes, and the fact tht you can do things like (\d{0..9}) or (\d*) to specifiy a certain number of digits.

    It always helps to read a little documentation before charging too deeply into new things! :)

    While the 'don't use regexes' comments are 100% right, a regex cleanup solution could look something like $x=~s/[0]?(\d+)/$1/;. The question mark means 'optional', and the '+' means 'one or more of'. The $1 represents what you matched inside parens between the first set of slashes. So we are replacing digits with a leading zero with just the digits.

    Again, read the docs, they are excellent. (as usual, all code is untested)

Re: Regex and leading zeros in numbers
by QM (Parson) on Mar 24, 2004 at 18:42 UTC
    If the numeric suggestions above don't solve your problem, read on...

    Matching usually involves the m// operator, while substitution usually involves the s/// operator. While it is often useful and efficient to use the s/// operator in an if{} statement, when starting out you might go a little slower.

    Assuming you have 2 variables, and you want to match the one (without leading zeros) to the other (with leading zeros), you might be looking for this:

    $var1 = '020055'; $var2 = '20055'; if ( $var1 =~ /(0*)($var2)/ ) { print "Found match: <$1><$2>\n"; } else { print "No match: \$var1<$var1> \$var2<$var2>\n"; }
    which yields:
    Found match: <0><20055>
    To be more helpful, we need to know more about your problem. For instance, are you reading each value from different files? Do the values have exactly 5 interesting digits?

    Quantum Mechanics: The dreams stuff is made of

Re: Regex and leading zeros in numbers
by swkronenfeld (Hermit) on Mar 24, 2004 at 16:25 UTC
    do you mean that you're trying to check if any two given generic numbers are equivalent, except that one of them has a leading zero? or are you specifically looking for those two numbers (20055 and 020055)?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://339440]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2023-02-05 04:33 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.