4

This is maybe the 100+1 question regarding regex optional suffixes on SO, but I didn't find any, that could help me :(

I need to extract a part of string from the common pattern:

prefix/s/o/m/e/t/h/i/n/g/suffix

using a regular expression. The prefix is constant and the suffix may not appear at all, so prefix/(.+)/suffix doesn't meet my requirements. Pattern prefix/(.+)(?:/suffix)? returns s/o/m/e/t/h/i/n/g/suffix. The part (?:/suffix)? must be somehow more greedy.

I want to get s/o/m/e/t/h/i/n/g from these input strings:

prefix/s/o/m/e/t/h/i/n/g/suffix
prefix/s/o/m/e/t/h/i/n/g/
prefix/s/o/m/e/t/h/i/n/g

Thanks in advance!

4
  • If this is the whole string, you can use ^prefix(.*?)(?:\/(?:suffix)?)?$ as a general pattern, not sure about the concrete implementation (and correct escaping) in java . Commented Sep 27, 2016 at 11:27
  • Yes, that it is, thanks a lot! Put it as an answer, please Commented Sep 27, 2016 at 11:29
  • Is the input allowed to contain characters before or after the match, i.e. something like ...prefix/s/o/m/e/t/h/i/n/g/suffix...? Commented Sep 27, 2016 at 11:29
  • @Thomas no, strings are strict prefixed. The suffix is also strict if any. The right pattern was just posted. Take a look on the comment earlier Commented Sep 27, 2016 at 11:32

2 Answers 2

7

Try

prefix\/(.+?)\/?(?:suffix|$)

The regex need to know when the match is done, so match either suffix or end of line ($), and make the capture non greedy.

See it here at regex101.

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

1 Comment

this doesn't match next example - prefix/s/o/m/e/t/h/i/n/g/suffixandmore
1

Try prefix(.*?)(?:/?(?:suffix|$)) if there are characters allowed before prefix of after suffix.

This requires the match to be as short as possible (reluctant quantifier) and be preceeded by one of 3 things: a single slash right before the end of the input, /suffix or the end of the input. That would match /s/o/m/e/t/h/i/n/g in the test cases you provided but would match more for input like prefix/s/o/m/e/t/h/i/n/g/suff (which is ok IMO since you don't know whether /suff is meant to be part of the match or a typo in the suffix).

5 Comments

That would only make the x in suffix optional.
@ClasG you're right, I'll fix that. Also forgot the /.
Due to greedy matching, the first group will always consume the whole string.
I suggest you test your RE's at regex101 or similar before posting them ;) That's not working either - see here.
@ClasG I tested it but my testing tool fooled me.

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.