4

I have a tricky regular expression and I can't succeed to implement it.

I need the regular expression for this :

AEBE52E7-03EE-455A-B3C4-E57283966239

I use it for an identification like this :

url(r'^user/(?P<identification>\<regular expression>)$', 'view_add')

I tried some expressions like these ones:

\[A-Za-z0-9]{8}^-{1}[A-Za-z0-9]{4}^-{1}[A-Za-z0-9]{4}^-{1}[A-Za-z0-9]{4}^-{1}[A-Za-z0-9]{12}
\........^-....^-....^-....^-............

Someone can help me?

Thanks.

1
  • 1
    What are all those circumflexes for? They make it impossible for your regex to match anything. And what's with the backslash at the beginning? (On an other note; {1} is unnecessary.) Commented Mar 6, 2015 at 17:33

2 Answers 2

4

Just remove all the ^ symbols present in your regex.

>>> s = 'AEBE52E7-03EE-455A-B3C4-E57283966239'
>>> re.match(r'[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$', s)
<_sre.SRE_Match object; span=(0, 36), match='AEBE52E7-03EE-455A-B3C4-E57283966239'>
>>> re.match(r'[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}$', s).group()
'AEBE52E7-03EE-455A-B3C4-E57283966239'

-{1} would be written as - It seems like all delimited words are hex codes. So you could use [0-9a-fA-F] instead of [A-Za-z0-9] .

>>> re.match(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$', s).group()
'AEBE52E7-03EE-455A-B3C4-E57283966239'
Sign up to request clarification or add additional context in comments.

1 Comment

Same remark: [0-9a-fA-F] is more appropriate for GUIDs.
1

You dont need ^ and for - dont need {1},you can use the following pattern :

\w{8}-\w{4}-\w{4}-\w{4}-\w{12}

Note that \w will match any word character (A-Za-z0-9)

Or :

\w{8}-(\w{4}-){3}\w{12}

And as mentioned in comment if you are using a UUID as a more efficient way you can use the following pattern :

[a-fA-F\d]{8}(-[a-fA-F\d]{4}){3}-[a-fA-F\d]{12}

DEMO

3 Comments

Come on, that's a GUID... [0-9a-fA-F] is much more appropriate than \w.
@LucasTrzesniewski where is A-F?
@LucasTrzesniewski aha, yeah i miss that its UUID!

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.