5

I have the following string

'abc[123]defgh ijk[456]lm no[78] pq'

And I would like to extract all parts which are either between the begin of the string and [ or between whitespace and [. For the given string, these are the parts 'abc', 'ijk', and 'no'.

I have the following expression

exp = re.compile(r'\s(.*?)\[')

But I cannot figure out how to add the beginning of the string as an optional expression. How do I have to write the expression to cover both cases?

2 Answers 2

7

Try this pattern:

(?:^|\s)(.*?)\[

The start anchor (^) matches the beginning of the string (or line in MULTILINE mode).

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

1 Comment

Thanks, this works, and after having a look at the documentation, I understand the syntax as well :-)
1

Another: after finding the starting character, look for everything that is NOT a [ and ensure it is followed by a [

(?:^|\s)([^\[]+)(?=\[)

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.