How to use pipe operator as part of regular expression?

I want to map url inside strings like

u1 = "Check this out http://www.cnn.com/stuff lol" u2 = "see http://www.cnn.com/stuff2" u3 = "http://www.espn.com/stuff3 is interesting" 

Something like the following works, but it is cumbersome because I have to repeat the whole template

 re.findall("[^ ]*.cnn.[^ ]*|[^ ]*.espn.[^ ]*", u1) 

In particular, in my real code, I wanted to map a much larger number of websites. Ideally, I can do something similar to

 re.findall("[^ ]*.cnn|espn.[^ ]*", u1) 

but of course it doesnโ€™t work right now because I am not correctly specifying the website name. How can this be done better? Thanks.

+6
source share
1 answer

Non-group correspondences.

 re.findall("[^ ]*.(?:cnn|espn).[^ ]*", u1) 
+6
source

Source: https://habr.com/ru/post/886588/


All Articles