使用正则表达式从“1.hello”获取“hello”

2024-04-29 13:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是在学习正则表达式,我在从列表中获取单词时遇到了困难

从如下列表中:

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

我想找回单词:“你好”、“盖洛”、“伙计”和“柳树”

这是我迄今为止的简化代码

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

老实说,我尝试了很多组合,但在网上找不到一篇我能理解的好文章。提前谢谢


Tags: 代码inhello列表for单词leftword
2条回答

您的正则表达式模式:

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

您正在查找一个或多个非空格'\S+'),这些数字后面跟一个句点,后面跟一个空格('\d+\.\s'),空格后面跟一个破折号('\s-'):

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

相关问题 更多 >