在字符串中搜索逗号(,),如果存在逗号,则在python中打印逗号后的直接单词

2024-06-16 12:52:26 发布

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

我是新的正则表达式匹配,我有如下字符串

"karthika has symptoms cold,cough her gender is female and his age is 45"

在第一个字符串匹配中,我将检查关键字“症状”,并选择关键字的下一个单词,如下所示:

regexp = re.compile("symptoms\s(\w+)")
symptoms = regexp.search(textoutput).group(1)

这将使症状值为“cold”,但我有多个症状在文本中出现,因此在第二步中,我需要在“cold”之后签入文本如果有逗号(,)出现,如果逗号出现意味着我需要使用正则表达式打印逗号i之后的值,即“咳嗽”。你知道吗

请帮我实现这个。。你知道吗


Tags: 字符串文本症状is关键字genderfemalehas
2条回答

您可以使用regex捕获组 例如

# the following pattern looks for 
# symptoms<many spaces><many word chars><comma><many word chars>

s_re = re.compile(r"symptoms\s+\w+,(\w+)")

完整代码是

import re
from typing import Optional

s_re = re.compile(r"symptoms\s+\w+,(\w+)")

def get_symptom(text: str) -> Optional[str]:
    found = s_re.search(text)

    if found:
      return found.group(1)
    return None

您可以使用正则表达式来查找'symptoms'之后的第一个单词,还可以选择使用以komma开头的更多匹配项、mabye空格和更多wordcharacters:

import re

pattern = r"symptoms\s+(\w+)(?:,\s*(\w+))*"
regex = re.compile(pattern)

t = "kathy has symptoms cold,cough her gender is female. john's symptoms  hunger, thirst."
symptoms = regex.findall(t)

print(symptoms)

输出:

[('cold', 'cough'), ('hunger', 'thirst')]

说明:

r"symptoms\s+(\w+)(?:,\s*(\w+))*"
# symptoms\s+                      literal symptoms followed by 1+ whitepsaces 
#            (\w+)                 followed by 1+ word-chars (first symptom) as group 1
#                 (?:,        )*   non grouping optional matches of comma+spaces
#                        (\w+)     1+ word-chars (2nd,..,n-th symptom) as group 2-n 

备用方式:

import re

pattern = r"symptoms\s+(\w+(?:,\s*\w+)*(?:\s+and\s+\w+)?)"

regex = re.compile(pattern)

t1 = "kathy has symptoms cold,cough,fever and noseitch her gender is female. "
t2 = "john's symptoms  hunger, thirst."
symptoms = regex.findall(t1+t2)

print(symptoms)

输出:

['cold,cough,fever and noseitch', 'hunger, thirst']

这只适用于“英式”英语-美国式的学习方式

"kathy has symptoms cold,cough,fever, and noseitch" 

只会导致cold,cough,fever, and匹配。你知道吗

您可以在','" and "分割每个匹配项,以得到您的单一原因:

sym = [ inner.split(",") for inner in (x.replace(" and ",",") for x in symptoms)] 
print(sym)

输出:

[['cold', 'cough', 'fever', 'noseitch'], ['hunger', ' thirst']]

相关问题 更多 >