Marco
Marco

Reputation: 101

Regular expression to match file names

I'm dealing with a simple problem I guess.

I have a Cognos Report that leaves two files in a folder, after that an interface has to pick up one of the files depending on the filename to start running.

The report process asks the user to set a name to the report. For ex. if I choose the name PaymentJournal06.09, it creates these files:

PaymentJournal 06.09-en-us-xml_desc.xml
PaymentJournal 06.09-en-us.xml

I need a regular expression to pick up only the second file. I tried with PaymentJournal*-en-us.xml but it doesn't work.

Upvotes: 1

Views: 6640

Answers (2)

andronikus
andronikus

Reputation: 4210

You're very close. The . is a special character in regular expressions which matches any one character. So where you had the * wildcard, you want a .* or a .+ instead. The first matches 0 or more of any character, and the second matches 1 or more, but both should work for this case. Then, the . you used needs to be escaped, like \., because you actually want to match a . there. Putting it all together:

PaymentJournal.*-en-us\.xml

Upvotes: 4

Paul Creasey
Paul Creasey

Reputation: 28824

with regular expressions the * is not a wildcard like you're used to, instead it is a quantifier meaning zero or more. So in your example:

PaymentJournal*-en-us.xml

l* means zero or more l characters, which would match:

PaymentJourna-en-us.xml
PaymentJournal-en-us.xml
PaymentJournall-en-us.xml
PaymentJournallll-en-us.xml
...

You need to use the . character which matches anything:

PaymentJournal.*-en-us.xml

Or you could be more specific:

PaymentJournal \d\d\.\d\d-en-us.xml

where \d matches a number and \. matches a period (.)

Upvotes: 6

Related Questions