How to mount a regex for certain strings c#?

Asked

Viewed 48 times

-3

How could I mount a regex for a random string. For example if I were to take every word that was in this format in a string,

02ef6308-72da-491a-86b3-884a9e6bd959

I did so, but it did not work to get this result. What is wrong?

Regex r = new Regex(@"[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}-");

  • There is even this - in the end?

  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

2


We use regex to search for patterns and not "random strings". In this case, it seems you want to search for Guids.

With the exception of a - at the end of the pattern, your regex looks correct, but could be simplified to [A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}, since there are 3 similar parts in the middle.

  • I understand how it simplifies. Thank you!

  • 1

    If it really is a GUID, it is better to use [A-Fa-f0-9] to only consider the letters from "A" to "F"

  • Truth @hkotsubo, there was no attack on me! I updated the answer. Tks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.