druuu
druuu

Reputation: 1716

Repeated pattern in python regex

New to python regex and would like to write something that matches this

<name>.name.<age>.age@<place>

I can do this but would like the pattern to have and check name and age.

pat = re.compile("""
       ^(?P<name>.*)
        \.
        (?P<name>.*)
        \.
        (?P<age>.*)
        \.
        (?P<age>.*?)
        \@
        (?P<place>.*?)
        $""", re.X)

I then match and extract the values. res = pat.match('alan.name.65.age@jamaica')

Would like to know the best practice to do this?

Upvotes: 1

Views: 65

Answers (3)

user6399774
user6399774

Reputation: 116

You dont need the groups if you use re.split :

re.split('\.name\.|\.age', "alan.name.65.age@jamaica")

This will return name and age as first two elements of the list.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338128

Match .name and .age literally. You don't need new groups for that.

pat = re.compile("""
       ^(?P<name>[^.]*)\.name
        \.
        (?P<age>[^.]*)\.age
        \@
        (?P<place>.*)
        $""", re.X)

Notes

  • I've replaced .* ("anything") by [^.]* ("anything except a dot"), because the dot cannot really be part of the name in the pattern you show.
  • Think whether you mean * (0-unlimited occurrences) or rather + (1-unlimited occurrences).

Upvotes: 3

Booboo
Booboo

Reputation: 44023

No reason not to allow . in names, e.g. John Q. Public.

import re

pat = re.compile(r"""(?P<name>.*?)\.name
                 \.(?P<age>\d+)\.age
                 @(?P<place>.*$)""",
                 flags=re.X)
m = pat.match('alan.name.65.age@jamaica')
print(m.group('name'))
print(m.group('age'))
print(m.group('place'))

Prints:

alan
65
jamaica

Upvotes: 2

Related Questions