Salvatore Cassano
Salvatore Cassano

Reputation: 3

Write a Regular Expression in Python to match substring

I'm working on a Python project to find into specified strings like:

< url: 'something', uid: 'something', type: 'something' >

and i need to get the values something. Example:

String value = "url: 'abs52fs', uid: '1fg23s4', type: 'fgh54e'"
re.search("url: '(.*)', uid: '(.*)', type: '(.*)'", value)

OUTPUT: >> [abs52fs, 1fg23s4, fgh54e]

I thought to write a regular expression but it returns a NoneType Object. Can you help me? Regards

Upvotes: 0

Views: 52

Answers (2)

Giacomo d&#39;Antonio
Giacomo d&#39;Antonio

Reputation: 2285

Your regular expression works, but it has a problem. It will wrongly accept strings like this:

url: 'abs52fs', url: 'abs52fs', uid: '1fg23s4', type: 'fgh54e'

(see https://regex101.com/r/sK0vN1/1)

Try something like this (https://regex101.com/r/xN7zR0/1):

url: '([^']*)', uid: '([^']*)', type: '([^']*)'

Upvotes: 0

Joe Young
Joe Young

Reputation: 5895

If you don't mind 3 separate calls:

>>> import re
>>> value = "url: 'abs52fs', uid: '1fg23s4', type: 'fgh54e'"
>>> url = re.search(r"url:\s'(.*?)'", value)
>>> url.group(1)
'abs52fs'
>>> uid = re.search(r"uid:\s'(.*?)'", value)
>>> uid.group(1)
'1fg23s4'
>>> type = re.search(r"type:\s'(.*?)'", value)
>>> type.group(1)
'fgh54e'

If you know your data will always follow the pattern url, followed by uid, followed by type, then you can do:

>>> tokens = re.search(r"url:\s'(.*?)',\suid:\s'(.*?)',\stype:\s'(.*?)'", value)
>>> url, uid, type = tokens.groups()
>>> url
'abs52fs'
>>> uid
'1fg23s4'
>>> type
'fgh54e'

Upvotes: 1

Related Questions