Search code examples
regexregex-lookaroundsregex-greedyboost-regexqregexp

Using Regex, how do I extract the numbers from this serial code?


I have over 1,000 serial codes I need to enter into a database but they have to be completely numerical for conversion identification purposes. They all look similar to this format but contain different characters/numbers:

d47a3c06-r188-4203-n838-fefd32082fd9

I've been trying to figure out how to use regex to remove all letters and dashes but I'm now at a loss.

I need to know how to turn this: d47a3c06-a188-4203-b838-fefd32082fc9

Into this: 473061884203838320829

Using regex. Then possibly trim it down to a 5 digit number using the first 5 numbers.

Thank you so much!


Solution

  • Since you are using Drupal, if what you need is an answer in PHP, then a PHP translation of the answer made by @jay-jargot is like this:

    $input = "d47a3c06-r188-4203-n838-fefd32082fd9";
    $str = preg_replace("/[^0-9]/", "", $input);
    $str = substr($str, 0, 5);
    echo $str, "\n";        ## output: 47306