1

I am trying to process historic military service numbers which have a very variable format. The key thing is to remove any prefix, but also to keep any suffix. Prefixes most commonly have a delimiter of a space, slash or dash, but sometimes they do not. In these cases the prefix is always one or more uppercase letters. In all other cases both prefixes and suffixes can contain letters or numbers and whilst typically uppercase, can be lower!

Currently my php code is

$cleanServiceNumber = preg_replace("/^.*[\/\s-]/","",$serviceNumber)

and typical values and desired results are

AB/12345 => 12345

CD-23456 => 23456

EF 34567 => 34567

5/45678 => 45678

GH/56789/A =>56789/A

GH/56789B => 56789B

XY67890 => 67890 <<< fails to do any replace and returns XY67890

I'm afraid my basic regex skills are failing me in terms of sorting the last example!

0

4 Answers 4

1

This regex replaces the combination of 0 to n digits and n non-digits at the beginning of the string: /^\d*\D+/

Demo

$serviceNumbers = array(
'AB/12345',
'CD-23456',
'EF 34567',
'5/45678',
'GH/56789/A',
'GH/56789B',
'XY67890');
foreach ($serviceNumbers as $serviceNumber) {
    $cleanServiceNumber = preg_replace("/^\d*\D+/","",$serviceNumber);
    echo $cleanServiceNumber . "\n";
}

Output:

12345
23456
34567
45678
56789/A
56789B
67890
0
0

You can add an alternation of [A-Z]+, but you should also make the other alternation more efficient by searching for non-delimiter characters followed by a delimiter:

$cleanServiceNumber = preg_replace("/^(?:[^\/ -]+[\/ -]|[A-Z]+)/","",$serviceNumber);

Demo on regex101

PHP demo on 3v4l.org

0

Here is another try for a regex which looks like:

/^([A-Za-z]+(\d+\W|\W)?|\d+\W)/

It has 2 parts which detects the type of prefixes you have:

  • [A-Za-z]+(\d+\W|\W)? => Any alphabets ending with non word character or alphabets having numbers and then ending with non word character. However, this ending game is optional with a ? at the end.
  • \d+\W => Any digits followed by a non word character.

Snippet:

<?php

$tests = [
        'AB/12345',
        'CD-23456',
        'EF 34567',
        '5/45678',
        'GH/56789/A',
        'GH/56789B',
        'XY67890',
        'XY67890/90/A'
    ];

foreach($tests as $test){
    echo $test," => ",preg_replace("/^([A-Za-z]+(\d+\W|\W)?|\d+\W)/","",$test),PHP_EOL;
}

Demo: https://3v4l.org/9hJLJ

0
0

The pattern you tried ^.*[\/\s-] first matches until the end of the string because the dot is greedy. Then it will backtrack until it can match either a /, - or a whitespace char.

This will not work for GH/56789/A as it will backtrack until the last / and it will not work for XY67890 as it does not match any of the characters in the character class.


You could match from the start of the string either 1 or more chars a-zA-Z or 1 or more digits 0-9 and at the end match an optional /, - or a horizontal whitespace character.

^(?:[A-Za-z]+|\d+)[/\h-]?

Regex demo | Php demo

For example

$serviceNumbers = [
    "AB/12345",
    "CD-23456",
    "EF 34567",
    "5/45678",
    "GH/56789/A",
    "GH/56789B",
    "XY67890"
];

foreach ($serviceNumbers as $serviceNumber) {
    echo preg_replace("~^(?:[A-Za-z]+|\d+)[/\h-]?~","",$serviceNumber) . PHP_EOL;
}

Output

12345
23456
34567
45678
56789/A
56789B
67890

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.