3

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

chris85
  • 23,846
  • 7
  • 34
  • 51
Hene
  • 159
  • 3
  • 13

1 Answers1

2

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:

  • \b - a leading word boundary (or (?<!\d) - a negative lookbehind making the regex fail if there is a digit before the current location)
  • [0-9]{5} - 5 digits
  • -1 - a literal char sequence -1
  • \b - a trailing word boundary (or (?!\d) - a negative lookahead making the regex fail if there is a digit right after the current location)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563