Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to get the first number after a certain string followed by any data until the number

Tags:

regex

php

I have a piece of data, retrieved from the database and containing information I need. Text is entered in a free form so it's written in many different ways. The only thing I know for sure is that I'm looking for the first number after a given string, but after that certain string (before the number) can be any text as well.

I tried this (where mytoken is the string I know for sure its there) but this doesn't work.

/(mytoken|MYTOKEN)(.*)\d{1}/
/(mytoken|MYTOKEN)[a-zA-Z]+\d{1}/
/(mytoken|MYTOKEN)(.*)[0-9]/
/(mytoken|MYTOKEN)[a-zA-Z]+[0-9]/

Even mytoken can be written in capitals, lowercase or a mix of capitals and lowercase character. Can the expression be case insensitive?

like image 635
Ben Fransen Avatar asked Feb 06 '23 22:02

Ben Fransen


1 Answers

You do not need any lazy matching since you want to match any number of non-digit symbols up to the first digit. It is better done with a \D*:

/(mytoken)(\D*)(\d+)/i

See the regex demo

The pattern details:

  • (mytoken) - Group 1 matching mytoken (case insensitively, as there is a /i modifier)
  • (\D*) - Group 2 matching zero or more characters other than a digit
  • (\d+) - Group 3 matching 1 or more digits.

Note that \D also matches newlines, . needs a DOTALL modifier to match across newlines.

like image 139
Wiktor Stribiżew Avatar answered Feb 10 '23 09:02

Wiktor Stribiżew