[英]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)
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.