Reputation: 3778
So here's my quandary:
^.*System(.*)(\d{4}-\d{2}-\d{2})?\.log
I need a regular expression that will match this optional group in parentheses (a date). However, since this group is optional, and the .+
is "eager", the date group never participates in the match. So I am trying to match things like these:
/var/log/ws/SystemOut.appserver204.log
/var/log/ws/SystemOut.appserver204.2014-10-22.log
/var/log/ws/SystemErr.appserver208.log
/var/log/ws/SystemErr.appserver212.2014-11-12.log
However, the first group, (.+)
, always takes everything up until the .log
, resulting in the optional date group never participating in the match. If I don't make the group optional, I start including the date, but then two of those cases mentioned above don't get matched.
How can I do what I am trying to accomplish? (For reference purposes, I am working in Java.)
Upvotes: 0
Views: 60
Reputation: 70732
*
is a greedy operator meaning it will match as much as it can and still allow the remainder of the regular expression to match. Use *?
for a non-greedy match meaning "zero or more — preferably as few as possible".
^.*System.*?(\d{4}-\d{2}-\d{2})?\.log
Upvotes: 1
Reputation: 27095
Look at this sample. The regex ^.*System(.+?)\.?(\d{4}-\d{2}-\d{2})?\.log
matches the text in Group 1 and the date in Group 2 (excluding the dot). A visualization:
Upvotes: 0