Do you have questions ? We got the answers!

Regex to match number not preceded by alphabets

Open 0 Answers 28 Views PHP

I want to get first number from the string before the word 'minimum', preceeded by a space and not succeeded by '-'. Example:

  • shouldn't match the words 2-3, TT-89 etc; (ie, a number followed by - and another number or alphabets followed by - and numbers)
  • should match the word 2-rolls (ie, a number followed by - and alphabets)

I'm trying with the following code:

$str = "Red  QQ-4555 White  TT-789 Yellow Minimum order applies. This is a test";
$explodeByMinimumArray = preg_split("/minimum/i", str_replace(array( '(', ')' ), ' ', $str));   
preg_match_all('/\d+(?!-\d)/', $explodeByMinimumArray[0], $numberFromStringBeforeMinimumArray);  

This is returning $numberFromStringBeforeMinimumArray as:

    [0] => Array
            [0] => 4555 
            [1] => 789


But the expected output is empty as QQ-4555 and TT-789 are preceeded by some characters.

Can anyone help me to fix this? Thanks in advance.

Your answer

Format: new
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

0 Answers