简体   繁体   English

用于匹配 XX/YY 字符串但不匹配 XX/YY/ZZ 字符串的正则表达式

[英]Regex for matching XX/YY strings but not XX/YY/ZZ strings

I would like to try and match text that is in the form of我想尝试匹配以下形式的文本

<some digits>/<some digits>

, but NOT , 但不是

<some digits>/<some digits>/<some digits>

As an example, if the text is例如,如果文本是

01/21

or或者

1/21

I would like to match those, but not我想匹配那些,但不是

1995/01/01

or或者

5/12/92

The background for this is that I am using Duckling to detect dates, and it detects dates like "12/21", but classifies this as MM/DD, whereas I want it to treat it as YY/MM, so I would like to correct these detections.这样做的背景是我使用 Duckling 来检测日期,它检测到像“12/21”这样的日期,但将其归类为 MM/DD,而我希望它把它视为 YY/MM,所以我想纠正这些检测。

How would I do this?我该怎么做? Thanks谢谢

Use利用

(?<!\d)(?<!\d/)\d+/\d+(?!/?\d)

See proof .证明

Explanation解释

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
    /                        '/'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    /?                       '/' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead

Just use \d{1,2}\/\d{1,2} .只需使用\d{1,2}\/\d{1,2} You can try your regular expressions here: https://regex101.com您可以在这里尝试您的正则表达式: https://regex101.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python XX实例没有属性YY - Python XX instance has no attribute YY 属性错误 XX 对象没有属性 YY - AtrributeError XX object has no atrribute YY AttributeError:“ YY”对象没有属性“ xx” - AttributeError: 'YY' object has no attribute 'xx" 在Python中以(XX-YY)形式对数字集进行排序 - Sort Set of Numbers in the form (XX-YY) in Python 如何使Django支持IETF语言标签(xx-YY格式)? - How to make Django support IETF language tag (xx-YY format)? 我该如何在python中制作一个脚本,该脚本在yy天每隔xx分钟执行一次,并且在时间到期后关闭文件 - How can i make an script in python that executes every xx minutes for yy days and after the time expires it closes the file 如何解决最后一层形状的不一致问题:“检查目标时出错:预期的density_具有xx形状但数组的形状为yy” - How to fix inconsistency in last layer shape “Error when checking target: expected dense_ to have shape xx but got array with shape yy” 正则表达式用于匹配重复字符串 - RegEx for matching repetitive strings 使用正则表达式和 pandas 匹配字符串 - Matching strings with regex and pandas Python regex 提取匹配字符串之间的字符串,包括匹配字符串 - Python regex extract strings between matching strings, including matching strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM