【问题标题】:Find Regex Give more than one result [duplicate]查找正则表达式给出多个结果[重复]
【发布时间】:2020-02-20 22:21:28
【问题描述】:

我有一个代码可以查找某些 URL 的信息 我怎样才能设置这个代码只给我一个字符串结果? 实际上这段代码给我带来了所有的结果,我只需要一个。 在下面的图像中,绿色矩形是正确的结果,但如果 url 多次包含该字符串,这将显示两个,红色矩形。

for idx,row in df.iterrows():
    url = row['e.URL'].replace('/v01/', '/depot/')
    x = urlopen(url)
    new = x.read()
    soup = BeautifulSoup(new, "lxml-xml")
    match = ''.join(re.findall(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup)))
    df.at[idx,'NEW_APP'] = match

下面的代码给我带来了所有的结果:

match = ''.join(re.findall(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup)))

参考下图:

【问题讨论】:

  • findall 更改为 search
  • 给我以下错误:
  • ----> 6 匹配 = ''.join(re.search(r"(?i)cl[a-zA-Z]{3}\d{5}", str (soup))) TypeError: can only join an iterable
  • 你不能在任何东西上加入一个字符串。你只会退回比赛吗? match = re.search(...)
  • 现在我这样设置代码:match = (re.search(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup ))) 但结果是这样的,我只需要代码:

标签: python regex string pandas


【解决方案1】:

如果您不想有多个匹配项,则可以使用re.search

found = re.search(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup))
match = found.group(0) if found else ''

或者你可以像现在一样使用 findall 但只使用第一个匹配项

matches = re.findall(r"(?i)cl[a-zA-Z]{3}\d{5}", str(soup))
match = matches[0] if matches else ''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多