jason jason - 1 year ago 63
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 Source

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


See the regex demo

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download