Edit2: It wasn't clear if your test data
contained pipes or not. I saw the pipes in
the regex and assumed you are searching
for pipe delim. Oh well.. not sure if below
helps.
Using regex to match text that's pipe delimited will need more alternations to pick up the beginning and ending columns.
What about another approach?
text='start piston|xxx|piston ring|xxx|piston cast|xxx|piston|xxx|stock piston|piston end'
j=re.split(r'\|',text)
k = [ x for x in j if x.find('piston') >= 0 ]
['start piston', 'piston ring', 'piston cast', 'piston', 'stock piston', 'piston end']
k = [ x for x in j if x.startswith('piston') ]
['piston ring', 'piston cast', 'piston', 'piston end']
k = [ x for x in j if x == 'piston' ]
['piston']
j=re.split(r'\|',text)
if 'piston ring' in j:
print True
> True
Edit: To clarify - take this example:
text2='piston1|xxx|spiston2|xxx|piston ring|xxx|piston3'
I add '.' to match anything to show the items matched
re.findall('piston.',text2)
['piston1', 'piston2', 'piston ', 'piston3']
To make it more accurate, you will need to use look-behind assertion.
This guarantees you match '|piston' but doesn't include the pipe in the result
re.findall('(?<=\|)piston.',text2)
['piston ', 'piston3']
Limit matching from greedy to first matching character .*?< stop char >
Add grouping parens to exclude the pipe. The match .*? is smart enough to detect if inside a group and ignores the paren and uses the next character as the stop matching sentinel. This seems to work, but it ignores the last column.
re.findall('(?<=\|)(piston.*?)\|',text2)
['piston ring']
When you add grouping you can now just specify starts with an escaped pipe
re.findall('\|(piston.*?)\|',text2)
['piston ring']
To search the last column as well, add this non-grouping match (?:\||$) - which means match on pipe (needs to be escaped) or (|) the end ($) of string.
The non-grouping match (?:x1|x2) doesn't get included in the result. An added bonus it gets optimized.
re.findall('\|(piston.*?)(?:\||$)',text2)
['piston ring', 'piston3']
Finally, to fix for the beginning of the string, add another alteration much like the previous one for end string match
re.findall('(?:\||^)(piston.*?)(?:\||$)',text2)
['piston1', 'piston ring', 'piston3']
Hope it helps. :)