开发者

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 f开发者_JAVA技巧iles:

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.


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 (.)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜