0

I have strings of the form:

greengrocerabc
sandwichcba
oscardcba

I'd like to lazily match abc, abcd and abcde, so I can get the first component. So something like

sub("^(.+)(abc|cba|dcba)", "\\1", "oscardcba") => "oscar"

However, regexp OR greedy matches cba and I get oscard

How can I lazy match this OR? The language is R, but it can can act like grep or perl.

1 Answer 1

4

Then make the quantifier non-greedy:

^(.+?)(abc|cba|dcba)

This way, the capture group will only contain the shortest possible match (which will not include abc, cba or dcba).

Further reading:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.