Q:

Substring extraction with pandas / regex

I'm trying to use the built in pandas method .str.extract to extract a substring from within a column in a dataframe I have imported. The entries within the column all follow this structure:
x.xx% Test1 Test2 Test3 XYZ|ZYX Oct 2018

So essentially it is always a float %, following by a string (which doesn't always have the same length of words), followed by a three letter code which is either XYZ or ZYX and a date afterwards.
I'm trying to extract the Test1, Test2 and Test3 from the example above, meaning I want to strip out the percentage at the beginning, and where XYZ|ZYX occur I want everything after gone (including the three letter code).
I've been reading up on regex all morning but I'm struggling a bit to build some code using pandas extract that can pull out exactly what I want. Any suggestions? Furthest I've gotten is the below, which only pulls through the percentages at the beginning (was trying to split it into three categories):
.str.extract('(\d\.\d+%.)') 

A:

You can use a pattern with a lookahead to determine when to stop matching.
([\w\s]+?)(?=\w{3}\|)'

Details
(               # first capture group
    [\w\s]+?    # match letters or whitespaces
)
(?=             # lookahead
    \w{3}       # fixed length 3 chars
    \|          # literal `|`
)

s = pd.Series(['x.xx% Test1 Test2 Test3 XYZ|ZYX Oct 2018'])
s.str.extract(r'([\w\s]+?)(?=\w{3}\|)', expand=False)

0     Test1 Test2 Test3 
dtype: object

