Home: Perl Programming Help: Regular Expressions:
Regex for consequtive integers



asif
New User

Jan 21, 2011, 3:20 PM


Views: 5164
Regex for consequtive integers

Hello,

Could you please help me with regular expressions for idenifying strings that have numerals in them which are consequtive integers. For eg. I need to identify the following strings with the regex pattern.

A12345

12

FB123

Your time is highly appreciated. Thanks,

Asif


BillKSmith
Veteran

Jan 21, 2011, 7:07 PM


Views: 5156
Re: [asif] Regex for consequtive integers

Do it in two steps. First extract the longest string of digits. Then test if that pattern is found in the string of consecutive digits.


Code
use strict; 
use warnings;
use Readonly;
Readonly::Scalar my $CONSECUTIVE => '0123456789';
while (<>){
my( $numeric_string ) = /(\d+)/xms;
if ($numeric_string and $CONSECUTIVE =~ /$numeric_string/) {
print;
}
}

Good Luck,
Bill