且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python正则表达式获取所有内容,直到字符串中的第一个点

更新时间:2023-02-17 23:41:05

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

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 上的演示