1

I have three possible forms that will go through a regular expression, and I'd like to match them all.

text1
text1 text2
text1 text2 ;text3

I've got so far (.*?)(?:\s)(.*) working for 'text1 text2', but I able to handle all three cases if the semicolon is present. Any ideas?

1
  • Your desired matching text is not really text1, text2, and text3, right? Can you give more explanation on the text forms to match? Commented Feb 21, 2012 at 20:58

1 Answer 1

5

The following should work, it would also put 'text1', 'text2', and 'text3' into the correct groups:

^(.+?)(?:\s(.*?)(?:\s;(.*))?)?$

See it working: http://www.rubular.com/r/IyPyF3wXLx

Here is an explanation:

^                # start of string
(.+?)            # put text1 in group 1
(?:              # start an optional non-capturing group
  \s(.*?)        # space followed by text2, put text2 in group 2
  (?:            # start an optional non-capturing group
    \s;(.*)      # space and semicolon, followed by text3, put text3 in group 3
  )?             # end of optional non-capturing group
)?               # end of optional non-capturing group
$                # end of string

The optional groups in the middle allow your regex to match text2 and text3 if they are present, but still match if they are not.

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.