0

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

1
  • 4
    Could you please add full code? Commented Nov 23, 2015 at 11:57

2 Answers 2

1

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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: '([^']*)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.