techvice
techvice

Reputation: 1301

Specific file name regex with optional text

I am trying to build a regular expression for a specific name with optional text in the middle. This alone is fairly easy:

^(pom)(.*?)([.]xml)$

However, there is one constraint I would like to have. This may be is possible, perhaps it isn't (I haven't been able to find anything like this). There can be additional text within the file name but if it is there, it has to be preceded with an underscore. The following example should help illustrate what I am trying to get:

pom.xml - SUCCEED
pomdxml - FAIL
pomd.xml - FAIL
pom_asdf.xml - SUCCEED
pom_.xml - FAIL

Thank you in advance for your knowledge and help!

Upvotes: 1

Views: 757

Answers (3)

Babak Naffas
Babak Naffas

Reputation: 12561

This also worked for me

^pom(_\w+)*([.]xml)$

Upvotes: 0

Amber
Amber

Reputation: 526623

Just use an optional group.

^(pom)(_.*)?(\.xml)$

Upvotes: 0

Jacob Eggers
Jacob Eggers

Reputation: 9322

Here you go:

^(pom)(_.+)?(\.xml)$

Upvotes: 1

Related Questions