Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-27-2007, 01:34 AM   #1
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Rep: Reputation: 30
regular expressions in perl-get amount of matching digits


A am having serious trouble with regular expressions in perl.I am trying to match a phone number with regular expressions and get back the best match.

Say i have a number 004478851234,I do a query in a db wich returns all regular expressions matching the criteria.No i want to find the best match using perl.Say i have two matches.

0044788[59] => matches 8 digits
00447[0789] => matches 6 digits

How can i from perl get back the number of matching digits,so that I now to take the first one.

if i trie something like $number =~ /^$regularexpr/ ,its going to return true for both,without me knowing which one to pick beacues their is no reference to the amount of matching digits.How can i get this back,so that i know whick one to pick?

Thanks for the help
Old 06-27-2007, 02:28 AM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,407

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
hopefully one of these will help: Perl regex tutorial Perl equiv of Linuxquestions
Old 06-27-2007, 08:45 AM   #3
LQ Newbie
Registered: Nov 2006
Location: Greece
Distribution: Ubuntu 6.06 LTS
Posts: 9

Rep: Reputation: 0
Take a look at the links provided by chrism01. Maybe you'll find somethink useful.
Else, you've to make it from scratch. And idea is split each match (say $match) into an array of single digits (say @array_match) and calculate:

@what_I_want = (0,0,4,4,7,8,8,5,1,2,3,4);
@best_match = (); #initialize it to be the first match
$match_rank = 0; #the number of same digits of the best match so far
$tmp = 0; #temporary variable
$sum = 0; #the match rank of an array
$i=0; #a counter for keeping track, which digit is currently checked
foreach $digit (@array_match)
  if ($digit != $best_match[$i]){$tmp = 0;} #if the current digit is not the same, keep 0
  else {$tmp = 1;} #else 1
  $sum += $tmp; #adds 1 if the digits are the same. $sum will hold at the end of that loop the number of same digits
if ($sum > $match_rank){@best_match = @array_match;$match_rank = $sum} # if the match is better, substitute it
#with the current match
Please forgive some perl misspells since I've not written perl for a while, but take a closer look at the algorithm.
Old 06-27-2007, 09:18 PM   #4
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47

#! /usr/bin/perl -w

use strict;

my $target = "004478851234";
my $longest=0;

my @regexes = (

my $i;
for $i (@regexes){
        # do the regex match
        $target =~ $i;
        # $& contains the match, the split creates a list of individual characters, scalar
        # takes a count of the array elements.
        my $length = scalar split //, $&;
        # take the longer of $longest or $length
        $longest = $longest > $length ? $longest : $length;

print "longest match: $longest\n";


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions, Perl, everything but a / xemous Programming 6 05-22-2006 10:09 AM
Perl regular expressions CyberJedi Programming 2 10-02-2005 11:17 AM
Perl regular expressions daYz Programming 6 08-11-2005 07:27 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 11:32 AM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 05:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration