c0deblack
c0deblack

Reputation: 43

Regular expression for matching account numbers

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

  1. Account Number : 123456
  2. Acct 4567
  3. Acct Number : 123-456-789
  4. Account Number 134-456-789
  5. Account#111111

I need a regex which checks each line starting with “Acc”, ignore white space and any special character and then ending with numeric.

I have written the following regex. The problem is that it matches some of the account number variants. example it matches #1 and #2 but not #3 and #4. The account numbers do not have fixed length and as you can see there are different variants of the string "Account Number"

Any help or advise on fine tuning this will be greatly appreciated.

(Acc[^0-9]*[0-9]*)

Upvotes: 4

Views: 3289

Answers (1)

Stephan
Stephan

Reputation: 43023

Here is a more precise regex:

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

Regular expression visualization

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

Upvotes: 6

Related Questions