Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

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 [email protected], I want a@b
in [email protected], I want a@b
in [email protected], I want a@b

What my code is giving me...

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

like image 313
ealeon Avatar asked Oct 02 '13 16:10

ealeon


3 Answers

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

like image 178
Rohit Jain Avatar answered Nov 05 '22 22:11

Rohit Jain


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

>>> s = "[email protected]"
>>> 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 = "[email protected]"
>>> try:
...     index = s.index('.')
... except ValueError:
...     index = len(s)
...
>>> print(s[:index])
a@b
like image 33
Jerry Avatar answered Nov 06 '22 00:11

Jerry


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 = [
    "[email protected]",
    "[email protected]",
    "[email protected]",
    ]

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).

like image 6
Escualo Avatar answered Nov 06 '22 00:11

Escualo