Re: Regular expression
by tirwhan (Abbot) on Dec 09, 2005 at 15:49 UTC
|
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] |
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' );
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] |