开发者

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

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.


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.


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).


Not using regexs, but maybe this can work? (I'm assuming ':' and '/' are special characters so it will remove the URL implicitly)

def good_word(word):
    import string
    for c in word:
        if not c in string.ascii_letters:
            return False
    return True

def clean_string(str):
    return ' '.join([w for w in input.split() if good_word(w)])

print clean_string("%he#llo, my website is: http://www.url.com/abcdef123")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜