Results 1 to 4 of 4
  1. #1

    easy regular expression

    hey guys, thank you for previous help. I again stuck on formating this regular expression?
    I did find the number i want just couldn't format it as i want. This regular expression is finding all phone number in one line the text look like this
    abcd 234-0987 more sentence between # again 456-123-0987

    OUTPUT
    234-0987 more sentence between ... 456-123-0987

    I am trying to chop all the sentence or non digit. But i will cut off the - dash too.

    FORMAT I WANT
    234-0987 \n
    456-123-0987

    if ($data =~ m/[\s](\d{3}\-\d{4})\s\w+([\s]\d+\-\d{3}\-\d{4})\s/gcx)
    {
    print "$&\n";
    $nospace =$&;
    $nospace =~ s/\^w+$/\n/g;
    print "$nospace";


    }

    I TRIED TO s/\D\\g BUT THIS WILL TAKE ALL THE NON-DIGIT INCLUDING - DASH! AND I CAN'T USE ^\w+$ I DOES NOT WORK EITHER.

  2. #2
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    if($data =~ /(\d{3}-\d{4}).*(\d{3}-\d{3}-\d{4})/) {


    Code:
    #!/usr/local/bin/perl
    
    my $test1 = "abcd 234-0987 more sentence between # again 456-123-0987";
    my $test2 = "234-0987 more sentence between # again 456-123-0987";
    
    checkstring($test1);
    checkstring($test2);
    
    sub checkstring {
      my ($string) = @_;
      if($string =~ /(\d{3}-\d{4}).*(\d{3}-\d{3}-\d{4})/) {
        print "$1\n$2\n";
      }
    }
    perl test.pl
    234-0987
    456-123-0987
    234-0987
    456-123-0987

  3. #3
    Thank you Slidey. I really appreciated your help with this regular. I am not familiar with $1$2 I just know the basic match. Your are a regular expression expert! Thanks and have a great day.

    best,
    Always


    Originally posted by Slidey
    if($data =~ /(\d{3}-\d{4}).*(\d{3}-\d{3}-\d{4})/) {


    Code:
    #!/usr/local/bin/perl
    
    my $test1 = "abcd 234-0987 more sentence between # again 456-123-0987";
    my $test2 = "234-0987 more sentence between # again 456-123-0987";
    
    checkstring($test1);
    checkstring($test2);
    
    sub checkstring {
      my ($string) = @_;
      if($string =~ /(\d{3}-\d{4}).*(\d{3}-\d{3}-\d{4})/) {
        print "$1\n$2\n";
      }
    }
    perl test.pl
    234-0987
    456-123-0987
    234-0987
    456-123-0987

  4. #4
    Join Date
    Apr 2003
    Location
    UK
    Posts
    2,560
    the $1 and $2 specify the arguments in between the rounded brackets, $1 for the first and $2 for the second..

    an alternative would be:

    if(my ($phone1, $phone2) = ($string =~ /(\d{3}-\d{4}).*(\d{3}-\d{3}-\d{4})/)) {

    which would automatically assign the 2 numbers as $phone1 and $phone2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •