phpregexpreg-matchstripos

string contains 5 numbers and line


I am looping trough a txt file which contains lines for example:

asdasdasd
lorem ipsum
lorem 12345-1
more lorem ipsum bacon

From this I need to know when the line founds text 12345-1

Lorem in the beging is inrelative.

Can I use stripos like

if (stripos('/^[1-9]{0,5}$-1/', $line) !== false)

I dont know the right regex. Of course the 12345 can be what ever, but its always 5 digits and ends with -1


Solution

  • To find a 5-digit chunk followed with -1, you may use

    /\b[0-9]{5}-1\b/
    

    Or, if word boundaries (\b) is too restrictive, use lookarounds:

    /(?<!\d)\d{5}-1(?!\d)/
    

    See the regex demo

    Use it with preg_match:

    if (preg_match('/\b[0-9]{5}-1\b/', $line))
    {
         echo "Found!";
    }
    

    Pattern details: