Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regular expression

by ashok (Sexton)
on Dec 09, 2005 at 15:37 UTC ( [id://515566]=perlquestion: print w/replies, xml ) Need Help??

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

In my application , Emp no creates in 7 digists like 5038062. Curretnly I am capturing with 5.* and able to. But I want to make it generic to capturen beging with what may be the no. I have tried with [0-9][0-9][0-9][0-9][0-9][0-9][0-9] and [0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9] options and failes. It looks the string is char. Can you then help me how to?

Edit: g0n - added code tags

Replies are listed 'Best First'.
Re: Regular expression
by tirwhan (Abbot) on Dec 09, 2005 at 15:49 UTC

    I think what you're looking for is

    m/(\d{7,})/;

    which will capture seven or more digits in a row and place them into $1. But it's hard to tell, it would be nice if you could try to phrase your question a little clearer, (and use code tags so other people don't have to add them to your post).


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Regular expression
by tphyahoo (Vicar) on Dec 09, 2005 at 15:44 UTC
    you might want to post to "seekers of perl wisdom" instead of perlmonks discussion. \d+ is a regex to capture a series of digits. \d{7} captures exactly seven digits. hope this helps.
Re: Regular Expression
by wazzuteke (Hermit) on Dec 09, 2005 at 15:51 UTC
    From your description, I think something like this should work:
    ... my $number = 5038062; # In your example my ( $grabbed ) = $var =~ /([0-9]{7})/; print "$grabbed\n"; ...
    Given the number assinged to the $number variable, you will be able to capture any of the first seven numbers, 0-9, and placed into the $grabbed element. This approach will return a result set as an array, therefore the parenthases are required.

    Another approach would be something like:
    ... my $number = 5038062; # In your example my ( $grabbed ) = $var =~ /(\d{7})/; print "$grabbed\n"; ...
    Where you are being less strict (kind-of) with the numbers, and are now requesting to capture the first seven digits (\d) rather than the first seven digits, 0-9.

    I hope this is what you were looking for... and good luck!

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );
Re: Regular expression
by CountZero (Bishop) on Dec 09, 2005 at 17:17 UTC
    The two regexes you give are equivalent to \d{7} and \d{1,7} respectively and they will both match a number of 7 digits. The second wil also match numbers of less than 7 digits. So your regex should not have failed. Perhaps this behaviour is caused by something else in your program. Can you show the program (if it is long, only the relevant parts).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Regular expression
by swampyankee (Parson) on Dec 09, 2005 at 15:53 UTC

    Well, one problem is neither of your regex will match your string; the first will match strings which include 0-9 seven times in succession; the second will match seven consecutive copies of 0-9 or 0- or any combination of these; neither will match 7 random digits.

    First suggestion: read the Perl documentation. Your regex are valid, but won't do what you want.

    Second, and more helpful suggestion try:

    /^\d{7}$/; which will match all strings which comprise exactly seven digits, with no non-numeric characters.

    gOn's insertion of code tags made my entire reply nonsense

    emc

    When in doubt, read the directions.

    Visit Netlib
      Well, one problem is neither of your regex will match your string; the first will match strings which include 0-9 seven times in succession; the second will match seven consecutive copies of 0-9 or 0- or any combination of these; neither will match 7 random digits.
      I'm not sure why you are saying that, but the OP's first pattern WILL match 7 random digits. The second will also match 7 random digits, but it will also match 1-6 random digits as well.

      Unfortunately, the OP is a bit unclear as to whether this sequence of 7 numbers should be isolated or part of a larger string.


      dsb
      This @ISA my( $cool ) %SIG

        The square braces weren't there when I wrote my reply; I think g0n inserted the code tags after my comment. 0-90-9... does look a bit different from [0-9][0-9]; and I managed to get confused.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found