简体   繁体   中英

Python regex to get everything until the first dot in a string

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.

in a@bc , I want a@b
in a@bcd , I want a@b
in a@bcde , I want a@b

What my code is giving me...

  • a@bc prints a@b
  • a@bcd prints a@bc
  • a@bcde prints a@bcd

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.


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)

Demo on ideone

You can use .find() instead of regex in this situation:

>>> s = "a@b.c"
>>> print(s[0:s.find('.')])
a@b

Considering the comments, here's some modification using .index() (it's similar to .find() except that it returns an error when there's no matched string instead of -1):

>>> s = "a@b.c"
>>> try:
...     index = s.index('.')
... except ValueError:
...     index = len(s)
...
>>> print(s[:index])
a@b

You can use the split method: split the string at the . character one time, and you will get a tuple of (before the first period, after the first period). The notation would be:

mystring.split(".", 1)

Then you can simply create a generator that "yields" the part you are interested, and ignores the one you are not (the _ notation). It works as follows:

entries = [
    "a@b.c",
    "a@b.c.d",
    "a@b.c.d.e",
    ]

for token, _ in (entry.split(".", 1) for entry in entries):
    print token

Output:

a@b
a@b
a@b

The documentation for the split method can be found online :

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

I recommend partition or split in this case; they work well when there is no dot.

text = "example@example.com"

print text.partition(".")[0]
print text.split(".", 1)[0]
import re
data='a@b.c.d.e'
re.sub('\..*','',data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM