Python正则表达式获取所有内容,直到字符串中的第一个点 [英] Python regex to get everything until the first dot in a string

查看:42
本文介绍了Python正则表达式获取所有内容,直到字符串中的第一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

find = re.compile("^(.*)\..*")
for l in lines:
    m = re.match(find, l)
    print m.group(1) 

我想对字符串中的任何内容进行正则表达式,直到第一个点.

I want to regex whatever in a string until the first dot.

a@b.c中,我想要a@b
a@b.c.d中,我想要a@b
a@b.c.d.e 中,我想要 a@b

in a@b.c, I want a@b
in a@b.c.d, I want a@b
in a@b.c.d.e, I want a@b

我的代码给了我什么...

What my code is giving me...

  • a@b.c 打印 a@b
  • a@b.c.d 打印 a@b.c
  • a@b.c.d.e 打印 a@b.c.d
  • a@b.c prints a@b
  • a@b.c.d prints a@b.c
  • a@b.c.d.e prints a@b.c.d

应该找到什么才能只得到 a@b?

what should find be so that it only gets a@b?

推荐答案

默认情况下,所有量词本质上都是贪婪的.从某种意义上说,他们将尝试尽可能多地消耗字符串.你可以通过在他们后面附加一个 ? 来让他们不愿意:

By default all the quantifiers are greedy in nature. In the sense, they will try to consume as much string as they can. You can make them reluctant by appending a ? after them:

find = re.compile(r"^(.*?)\..*")

如评论中所述,如果您的字符串中没有 句点,则此方法将失败.因此,这取决于您希望它的行为方式.但是如果你想在这种情况下获得完整的字符串,那么你可以使用否定字符类:

As noted in comment, this approach would fail if there is no period in your string. So, it depends upon how you want it to behave. But if you want to get the complete string in that case, then you can use a negated character class:

find = re.compile(r"^([^.]*).*")

遇到第一个句点或字符串末尾会自动停止.

it will automatically stop after encountering the first period, or at the end of the string.

你也不想使用 re.match() 那里.re.search() 应该没问题.您可以将代码修改为:

Also you don't want to use re.match() there. re.search() should be just fine. You can modify your code to:

find = re.compile(r"^[^.]*")

for l in lines:
    print re.search(find, l).group(0)

ideone 上的演示

这篇关于Python正则表达式获取所有内容,直到字符串中的第一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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