Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex to match begin of string or whitespace

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?

like image 665
stefangachter Avatar asked Jan 19 '14 19:01

stefangachter


People also ask

How do you match the beginning of a string in Python?

Python Re Start-of-String (^) Regex. You can use the caret operator ^ to match the beginning of the string. For example, this is useful if you want to ensure that a pattern appears at the beginning of a string.

Which regex matches only a whitespace character in Python?

\s | Matches whitespace characters, which include the \t , \n , \r , and space characters. \S | Matches non-whitespace characters.

How do you match a string at the the beginning of a line?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


2 Answers

Try this pattern:

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

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

like image 123
p.s.w.g Avatar answered Oct 06 '22 16:10

p.s.w.g


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

(?:^|\s)([^\[]+)(?=\[)
like image 33
Ron Rosenfeld Avatar answered Oct 06 '22 15:10

Ron Rosenfeld