c# - 正则表达式捕获 "-"字符

标签 c# regex

我有这个字符串:

Ü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.

我得到日期时间:像这样:

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

我正在尝试像这样得到 254 :

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

我可以 catch 254123/456,但现在我还需要 catch 123-456。我怎样才能捕获它?谢谢。

最佳答案

-/ 添加到字符类 [-/] 中,以匹配单个 - 或单个 /:

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

请参阅regex demo

enter image description here

关于c# - 正则表达式捕获 "-"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39247307/

相关文章:

c# - 如何在 C# 中使用 Quickbase API 上传文件?

c# - 在 c# winform 中更改的文本上启用禁用按钮

javascript - 使用 .match() 返回未定义的 0

c# - 如何避免回车解析 XML

c# - 在 Unity 中,如何在外部应用程序中打开文件(即 PDF)?

正则表达式检查重复

php - 大括号之间的 Preg 匹配 - preg_match

Javascript - 如何使用 RegExp 识别模式

javascript - 通过搜索查找文本并突出显示出现错误

c# - 在 C# 中将 APRIL, 03/2013 字符串转换为 DateTime