Regular expression for matching account numbers

I need to match the account numbers from a large text file. Account numbers will be in the following format:

  • Account Number: 123456
  • Acct 4567
  • Acct Number: 123-456-789
  • Account Number 134-456-789
  • Account # 111111

I need a regular expression that checks every line starting with "Acc", ignores the space and any special character, and then ends with a numeric one.

I wrote the following regular expression. The problem is that it matches some account number options. For example, this corresponds to # 1 and # 2, but not # 3 and # 4. Account numbers do not have a fixed length, and, as you can see, there are different options for the "Account Number" line

Any help or recommendation on fine tuning would be greatly appreciated.

(Acc[^0-9]*[0-9]*)
+4
source share
1 answer

Here is a more accurate regular expression:

^Acc(?:oun)?t(?:\s+Number)?.+[\d-]+$

Regular expression visualization

DEMO: https://regex101.com/r/bM5yB2/4

+5
source

Source: https://habr.com/ru/post/1622705/


All Articles