正则表达式匹配,返回剩余的字符串 [英] Regex match, return remaining rest of string

查看:79
本文介绍了正则表达式匹配,返回剩余的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

匹配字符串开头的简单正则表达式函数Bananas:";并返回第二部分.我已经完成了正则表达式,但这不是我期望的工作方式:

Simple regex function that matches the start of a string "Bananas: " and returns the second part. I've done the regex, but it's not the way I expected it to work:

import re

def return_name(s):
  m = re.match(r"^Bananas:\s?(.*)", s)

  if m:
    # print m.group(0)
    # print m.group(1)
    return m.group(1)

somestring = "Bananas: Gwen Stefani" # Bananas: + name

print return_name(somestring) # Gwen Stefani - correct!

但是,我确信您没有使用 (.*) 来标识组以获得相同的结果.即匹配字符串的第一部分 - 返回剩余部分.但我不知道该怎么做.

However, I'm convinced that you don't have identify the group with (.*) in order to get the same results. ie match first part of string - return the remaining part. But I'm not sure how to do that.

另外我在某处读到你应该谨慎使用 .* 在正则表达式中.

Also I read somewhere that you should be being cautious using .* in a regex.

推荐答案

您可以使用 lookbehind ((?<=)):

(?<=^Bananas:\s).*

记住使用 re.search 而不是 re.match 因为后者会尝试匹配字符串的开头(也就是隐式 ^).

Remember to use re.search instead of re.match as the latter will try to match at the start of the string (aka implicit ^).

至于 .* 问题 - 如果您对正则表达式的工作方式没有清楚的了解,它可能会导致很多回溯,但在这种情况下,它可以保证是线性搜索.

As for the .* concerns - it can cause a lot of backtracking if you don't have a clear understanding of how regexes work, but in this case it is guaranteed to be a linear search.

这篇关于正则表达式匹配,返回剩余的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆