Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: How to find a GUID in a long string?

Tags:

python

regex

Let's say I have this string:

str = 'something-rows-1973912739821738172-25892e17-80f6-415f-9c65-7395632f0223'

I need to remove GUID part:

25892e17-80f6-415f-9c65-7395632f0223

This is what I've so far but it isn't working:

c = re.compile('[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}\Z', re.I)
res = c.match(str)
print(res)

Can anyone please help?

like image 989
90abyss Avatar asked Feb 05 '17 02:02

90abyss


People also ask

What characters can be in a GUID?

The valid GUID (Globally Unique Identifier) must specify the following conditions: It should be a 128-bit number. It should be 36 characters (32 hexadecimal characters and 4 hyphens) long. It should be displayed in five groups separated by hyphens (-).

What is Regex in str replace?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.


1 Answers

From this answer, you need this:

[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}

Working example: https://regex101.com/r/6pA9Rk/1

like image 94
ketan vijayvargiya Avatar answered Sep 19 '22 12:09

ketan vijayvargiya