-1

I'm looking to grab an ID number from an email subject line and almost have the correct expression, but it it only works when the ID is at the start of the line.

The regex needs to match:

  • Numbers only
  • 6 characters
  • Begins with 4

At the moment I have:

/(^4[0-9]{5})/

Examples Subject Lines:

This works: '406677 - Your reference'

This doesn't: 'Your reference 406677'

What do I need to change to select the ID from any position it may appear in the subject line?

Thanks

User787665
  • 237
  • 4
  • 23

2 Answers2

1

you just have to remove the carret (^) which means begining of the line

/\b(4[0-9]{5})\b/

Also to match only 6 digit "words" you have to add \b which means word boundary.

see https://www.phpliveregex.com/p/pfE

ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
0

Remove the carret and check there're no digits before and after with lookaround:

/(?<!\d)4\d{5}(?!\d)/
Toto
  • 89,455
  • 62
  • 89
  • 125