-1
My string is =

"Lead and opportunity scoring with advanced scoring models to customers most 
likely to convert and buy and Improve conversion and win rates"

I am trying to get all the characters in between

with|using|through and to|so|that [we considered with|using|through and to|so|that as groups.]

Executed:

pattern="to|so|now (.*) with|using|through"

x=re.findall(pattern,str)

And found of the results as

 x= ['', '']. Why is that?
anurag
  • 79
  • 1
  • 7

2 Answers2

2

When you use the | operator, you need to put your expression in a group like so:

(to|so|now) (.*) (with|using|through)

If you don't want to capture these groups, use non-capturing groups :

 (?:to|so|now) (.*) (?:with|using|through)
rolf82
  • 1,531
  • 1
  • 5
  • 15
  • Thanks for the answer. The returned variable is a list, but there is tuple inside it. So I couldn't convert into string format. [('with', 'advanced scoring models to customers most likely', 'to')] in variable output. So, I had to convert it into a tuple by : newTuple=output[0] Then I converted it into a string: strTuple=' '.join(newTuple) And it worked. Thanks. – anurag Feb 25 '20 at 05:27
  • Thanks for the feedback. It is not that you "convert it into a tuple", but you take the first element of the list, which is the first match. It is fine to take output[0] if you are sure that there is one match. As the regex is greedy you should not have more than one, so no need to loop over output, but there may be none, so consider checking output's length before taking an element. – rolf82 Feb 25 '20 at 13:38
0

Try with (to|so|now) (.*) (with|using|through). But, your example string contains no match indeed. Try with

"Lead and opportunity scoring with advanced scoring models to customers most 
likely to convert and buy and Improve conversion and win rates using hacks"

Oh and, https://regex101.com/ is your friend.

Cedric Druck
  • 1,032
  • 7
  • 20