Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex. Match words that contain special characters or 'http://'

Tags:

python

regex

I want to match words that contain special characters or that begin with 'http://'

So this sentence

%he#llo, my website is: http://www.url.com/abcdef123

should turn into this

my website

So far, i have this

re.sub(r"^[^\w]", " ", "%he#llo, my website is: http://www.url.com/abcdef123")

This just removes the symbols, but it doesn't remove the words associated with the symbol (it also doesn't remove ':' and ','), nor does it remove the URL.

like image 971
user216171 Avatar asked Dec 16 '22 18:12

user216171


2 Answers

For the example string you give, the following regular expression works OK:

>>> a = '%he#llo, my website is: http://www.url.com/abcdef123'
>>> re.findall('(http://\S+|\S*[^\w\s]\S*)',a)
['%he#llo,', 'is:', 'http://www.url.com/abcdef123']

... or you can remove those words with re.sub

>>> re.sub('(http://\S+|\S*[^\w\s]\S*)','',a)
' my website  '

The | means alternation and will match the expression on either side within the group. The part on the left matches http:// followed by one or more non-space characters. The part on the right matches zero or more non-space characters, followed by anything that isn't a word or space character, followed by zero or more non-space characters -- that ensures that you have a string with at least one non-word character and no spaces.

Updated: Of course, as the other answers implicitly suggest, since the http:// prefix contains a non-word character (/) you don't need to have that as an alternative - you could simplify the regular expression to \S*[^\w\s]\S*. However, perhaps the example above with alternation is still useful.

like image 139
Mark Longair Avatar answered Mar 08 '23 18:03

Mark Longair


You can use look aheads:

>>> re.findall(r"(?:\s|^)(\w+)(?=\s|$)", "Start %he#llo, my website is: http://www.url.comabcdef123 End")
['Start', 'my', 'website', 'End']

Explanation:

  • (?:\s|^) means our word starts the regex or is preceeded by a space. (and the space does not belong to the word).
  • (\w+) matches a word (and is what we are interested in).
  • (?=\s|$) means our word is followed by space or end of the string. (and once again, the space does not belong to the word).
like image 40
Antoine Pelisse Avatar answered Mar 08 '23 19:03

Antoine Pelisse