LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 06-27-2007, 02:34 AM   #1
baddah
Member
 
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


Hi,

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, 03:28 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,310

Rep: Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039
hopefully one of these will help:

http://perldoc.perl.org/perlretut.html Perl regex tutorial
http://www.perlmonks.org Perl equiv of Linuxquestions
 
Old 06-27-2007, 09:45 AM   #3
manosagn
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:

Code:
@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
  $i++;
}
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, 10:18 PM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Hm...

Code:
#! /usr/bin/perl -w

use strict;

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

my @regexes = (
        qr(00447[0789]),
        qr(0044788[59])
);

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";
 
  


Reply


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 11:09 AM
Perl regular expressions CyberJedi Programming 2 10-02-2005 12:17 PM
Perl regular expressions daYz Programming 6 08-11-2005 08:27 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 12:32 PM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 06:16 PM


All times are GMT -5. The time now is 11:45 PM.

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