MikeW
MikeW

Reputation: 11

Using Regex for a Purchase Order

I'm not sure this is even possible

I was given a requirement to match Purchase Order numbers within a filename. His requirement is below:

I would say a minimum of 4 characters, with no max limit. However there MUST be a string of 3 consecutive LETTERS (minimum) somewhere in this section.

123ABC - match A123ABC - match ABC123 - match 123XY67 - NO match ABC1VJ6K - match

The best I can come up with is [0-9A-Z]{4,}; however, that matches basically ANY 4 characters

Upvotes: 1

Views: 773

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

you can use this pattern:

[A-Z0-9]*?[A-Z]{3}[A-Z0-9]+|[A-Z0-9]+?[A-Z]{3}[A-Z0-9]*

An other way with a lookahead:

(?=[A-Z0-9]*[A-Z]{3})[A-Z0-9]{4,}

And why not, if your regex engine allows conditional:

([A-Z0-9]+?)?[A-Z]{3}(?(1)|[A-Z0-9])[A-Z0-9]*

Upvotes: 2

Related Questions