Given the input text
RANDOMNUMBERS Can I have some milk please
or
RANDOMNUMBERS Can I have some milk
I need to match both strings and capture only the Can I have some milk
part omitting the please if it exists and putting the content in the SAME capture group whether or not the please is present.
Assuming [RANDOMNUMBERS] can I have
will always be there, anything else can come afterwards. [RANDOMNUMBERS]
and please
should be omitted from the capture group if it’s there.
e.g. RANDOMNUMBERS Can I have more toast please
and RANDOMNUMBERS Can I have more toast
should yield Can I have more toast
for both as the SAME capture group number
the closest I’ve come to solving this is with
(?(?=(?!.*please).*Can I have).*(Can I have.*)|.*(Can I have.*)( please))
The above does capture the string that I want Can I have more toast
whether or not the please
exists however the captured phrase is put in different group numbers i.e group 1
when please
is omitted and group 2
when please
is present.