Regex get text before and after a hyphen

Oliver Oliver picture Oliver Oliver · May 12, 2016 · Viewed 28.1k times · Source

I have this string:

"Common Waxbill - Estrilda astrild"

How can I write 2 separate regexes for the words before and after the hyphen? The output I would want is:

"Common Waxbill" 

and

"Estrilda astrild"

Answer

Tim Pietzcker picture Tim Pietzcker · May 12, 2016

This is quite simple:

.*(?= - )     # matches everything before " - "
(?<= - ).*    # matches everything after " - "

See this tutorial on lookaround assertions.