jason jason - 3 months ago 11
C# Question

Regex to catch "-" character

I have this string :

Ümraniye Tapu Müdürlüğünde ve Ümraniye Belediyesi İmar Müdürlüğünde 20.08.2014 onay tarih ve 254 sayılı mimari projesi incelenmiştir.


I'm getting datetime : like this :

t = DateTime.Parse(Regex.Match(mimaristring, @"\d(\d+)[-.\/](\d+)[-.\/](\d+)").Value);


I'm trying to get 254 like this :

num = Regex.Match(mimaristring, @"(?<!\d\.)\b\d+(?:/\d+)?\b(?!\.\d)").Value;


I can catch 254 or 123/456 but now I also need to catch 123-456. How can I catch that? Thanks.

Answer

Add a - and / to the character class [-/] that will match either a single - or a single /:

(?<!\d\.)\b\d+(?:[/-]\d+)?\b(?!\.\d)
                 ^^^^

See the regex demo

enter image description here

Comments