Home > Software engineering >  How to regex for alphanumeric pattern with specific string
How to regex for alphanumeric pattern with specific string

Time:09-03

Needle in haystack: Specific string followed by exact set of numbers

How can I search for ABC???? where ABC should be exactly that, but the ???? must be exactly four numbers, ideally followed by whitespace.

Illustrative examples:

  • LHRJFKABC1234 233 <-- Has needle
  • EABC123 LHRJFK <-- Does not have needle as only 3 numbers following ABC

Something tells me I need to search for string something like (\d{4}) for the 4 numbers. But not sure quite how to puzzle it all together.

What I've found so far:

CodePudding user response:

For things like this I find an online checker like Rubular very handy.

Unless I'm misunderstanding, the regex ABC\d{4}\s should work for you. Do you need groupings (i.e. to match the 4-digit part)?

Try it out on Rubular here

  • Related