I'm trying to group 2 sub-sentences of whatever reasonable length separated by a specific word (in the example "AND"), where the second can be optional. Some example:
CASE1:
foo sentence A AND foo sentence B
shall give
"foo sentence A" --> matching group 1
"AND" --> matching group 2 (optionally)
"foo sentence B" --> matching group 3
CASE2:
foo sentence A
shall give
"foo sentence A" --> matching group 1
"" --> matching group 2 (optionally)
"" --> matching group 3
I tried the following regex
(.*) (AND (.*))?$
and it works but only if, in CASE2, i put an empty space at the final position of the string, otherwise the pattern doesn't match. If I include the space before "AND" inside round brackets group, in the case 1 the matcher includes the whole string in the first group. I wondered aroung lookahead and lookbehind assertions, but not sure they can help me. Any suggestion? Thanks