3

给定一个字符串...

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

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

例如'turt'

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

4

1 回答 1

7
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-+=[]}{@##:)']
于 2013-04-10T13:18:30.853 回答