Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Need help extracting numbers from a file

Last post 01-07-2009, 10:01 AM by ddrudik. 4 replies.
Sort Posts: Previous Next
  •  01-06-2009, 6:37 PM 49911

    Need help extracting numbers from a file

    I'm writing a script that strips properly formatted numbers out of text.  For some reason it does not seem to be working right.  I'm trying to find all 15 digit numbers in the file and have constructed the following expression: 

     /\b([0-9]{15})\b/

    The string is being used as follows in php5:

    preg_match_all( '/\b([0-9]{15})\b/', $file_contents, $match);
    $number_array=$match[1];

    Here is a short segment of the file that I'm passing to the script.

    random text with 398639164013360 numbers inserted in the  >398639164013377 middle to see whether 398639164013384 this stupid script works 398639164013391 

    What am I doing wrong???  Thanks!

  •  01-06-2009, 7:37 PM 49912 in reply to 49911

    Re: Need help extracting numbers from a file

    PHP Code Example:
    <?php
    $sourcestring="your source string";
    preg_match_all('/\b([0-9]{15})\b/',$sourcestring,$match);
    echo "<pre>".print_r($match,true);
    ?>

    $matches Array:
    (
        [0] => Array
            (
                [0] => 398639164013360
                [1] => 398639164013377
                [2] => 398639164013384
                [3] => 398639164013391
            )

        [1] => Array
            (
                [0] => 398639164013360
                [1] => 398639164013377
                [2] => 398639164013384
                [3] => 398639164013391
            )

    )

    \b is problematic to use, since [a-zA-Z_] are in \w as well as [0-9] so if you have numbers like this:

    A398639164013360 or 398639164013360A they will not be found, not sure if that's an issue for you.  If that is an issue an alternate pattern to use:

    PHP Code Example:
    <?php
    $sourcestring="your source string";
    preg_match_all('/(?<=^|\D)[0-9]{15}(?=\D|$)/',$sourcestring,$match);
    echo "<pre>".print_r($match,true);
    ?>
     

    Note that the capture group 1 is not necessary as you can reference $match[0] to access the same data, I eliminated capture group 1 from the last pattern.


  •  01-07-2009, 12:13 AM 49928 in reply to 49911

    Re: Need help extracting numbers from a file

    Excellent advice!  Thanks.  This forum is quickly becoming one of my favorites... 
  •  01-07-2009, 1:18 AM 49931 in reply to 49912

    Re: Need help extracting numbers from a file

    One follow-up question.  Why does the following regex statement match to those numbers as well. 

    /\b(\d\d\d\d\d\d\d\d\d\d?)\b/

    It returns the following, when I would expect it to not find any matches at all:


    $matches Array:
    (
        [0] => Array
            (
                [0] => 164013360
                [1] => 164013377
                [2] => 164013384
                [3] => 164013391
            )

    Thanks!

  •  01-07-2009, 10:01 AM 49934 in reply to 49931

    Re: Need help extracting numbers from a file

    Show the source string you are testing with.
View as RSS news feed in XML