Ramchander
Ramchander

Reputation: 17

Query on regular expression

I need to build up a regular expression to match the following file name pattern:

EDI*.DAT

and at the same time it should exclude files with the following file name patterns as EDI*US*.DAT and EDI*CA*.DAT

For example: Assume there are three files

"EDIabUS674.DAT" "EDIabcdCA1234.DAT" and "EDIabcIN987.DAT "

I need a regular expression to match for the file with name "EDIabcIN123.DAT" and at the same time should not match and exclude other two files "EDIabUS674.DAT" and "EDIabcdCA1234.DAT"

Any help on this is much appreciated!!

Upvotes: 0

Views: 54

Answers (1)

The fourth bird
The fourth bird

Reputation: 163457

Maybe you can use 2 negative lookaheads to assert that the string does not contain CA and US:

^(?!.*CA)(?!.*US)EDI\w+\.DAT$

Explanation

  • From the beginning of the string ^
  • Assert that what follows is not AC (?!.*CA)
  • Assert that what follows is not US (?!.*US)
  • Match EDI
  • match any word character one or more times \w+ (You can change this to for example [A-Za-z\d]+ if you don't want to match underscores)
  • Match .DAT \.DAT
  • The end of the string $

Upvotes: 1

Related Questions