如何通过Python中的RegEx在字符串中查找具有相同前缀的单词

时间:2013-04-10 13:16:47

标签: python regex

给出一个字符串......

truth = "I like turtles, turtles4756-+=[]}{@##:) I like"

你怎么能得到以已知前缀开头的单词数组?

e.g。 'turt'

["turtles", "turtles4756-+=[]}{@##:)"]

1 个答案:

答案 0 :(得分:5)

In [1]: import re

In [2]: truth = "I like turtles, turtles4756-+=[]}{@##:) I like"

In [3]: re.findall?
    Definition: re.findall(pattern, string, flags=0)
    ...
    Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result.

# [word boundary]turt followed by word characters
In [4]: re.findall(r'\bturt\w*', truth)
Out[4]: ['turtles', 'turtles4756']

# [word boundary]turt followed by non-whitespace characters
In [5]: re.findall(r'\bturt\S*', truth)
Out[5]: ['turtles,', 'turtles4756-+=[]}{@##:)']

In [10]: truth = "I like turtles, turtles4756-+=[]}{@##:) I like superturtles"

In [11]: re.findall(r'turt\S+', truth)
Out[11]: ['turtles,', 'turtles4756-+=[]}{@##:)', 'turtles']

In [12]: re.findall(r'\bturt\S+', truth)
Out[12]: ['turtles,', 'turtles4756-+=[]}{@##:)']