Reputation: 75
In my project, I can have two sentences like:
1) <keyword1> <start_date> <keyword2> <end_date>
2) <keyword1> <end_date>
For example:
1) "Away 01.09.2000 until 03.09.2000"
2) "Away 01.09.2000"
I need to find a match for <end_date>
against a value I posses - let's call it <the_date>
. I do not know which version of a sentence I have currently, so I need to cover both cases. If <the_date>=03.09.2000
I want the match with sentence 1), but if <the_date>=01.09.2000
I want the match with sentence 2).
I tried this regex (it expects spaces to be removed in the sentence), but it seems not working:
\bAway\b([0-9]{2}\.[0-9]{2}\.[0-9]{4}\buntil\b<the_date>|<the_date>(?:(?!\buntil\b))*?)
I know there are many similar questions, but I checked a few and none worked for me.
Thank you and appreciate your effort.
Upvotes: 1
Views: 47
Reputation: 626845
You can consider a pattern like
^Away(?:\s+([0-9]{2}\.[0-9]{2}\.[0-9]{4})\s+until)?\s+01\.09\.2000$
See the regex demo
Details
^
- start of stringAway
- a word(?:\s+([0-9]{2}\.[0-9]{2}\.[0-9]{4})\s+until)?
- an optional occurrence of
\s+([0-9]{2}\.[0-9]{2}\.[0-9]{4})
- 1+ whitespace chars and then Group 1 capturing a sequence of two digits, .
, two digits, .
and four digits\s+until
- 1+ whitespace chars and until
word\s+
- 1+ whitespace chars01\.09\.2000
- a 01.09.2000
string$
- end of string.Upvotes: 1