从“% 1.使用Regex

trnvg8h3  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(136)

我刚刚学习正则表达式,我有麻烦从列表中获取单词
从一个列表中,如:

[ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]

我想检索的话:“你好”、“gello”、“fellow”和“杨柳”
下面是我目前为止的简化代码

for i in [ARRAY OF LISTED WORDS]:
  word = re.findall(r'^((?![0-9]?[0-9]. ))\w+', i)
  print(word)
5n0oy7gb

5n0oy7gb1#

您正在查找一个或多个数字之间的 * 非空格 *('\S+'),后面是句点和空格('\d+\.\s'),后面是空格和破折号('\s-'):

pattern = r'\d+\.\s(\S+)\s-'
[re.findall(pattern, l)[0] for l in your_list]
wfauudbj

wfauudbj2#

你的正则表达式模式:

pattern = r"""
    \d+     # 1 or more digits
    \.      # Escaped period character
    \s+?    # 1 or more whitespace
    (\w+)   # 1 or more alphabetic characters
    \s+     # 1 or more whitespace
    -       # hyphen
    .*      # zero or more of anything besides newline.
"""

字符串列表:

words = [ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]

for word in words:
    # capture results in a variable
    # re.X for verbose pattern format.
    tmp = re.search(pattern, word, flags = re.X)
    # If variable is not None, print results of the first captured group.
    if tmp:
        print(tmp.group(1))

输出:

hello
gello
fellow
willow

相关问题