Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将某些子字符串(例如月份中的日期)匹配为字符串中的“21st”或“22nd”或“23rd”,因此我使用此模式制作了一个正则表达式:
((\d{1,2})(st)|(nd)|(rd)|(th)).
我制作这些组是因为我想做替换。但是当我匹配一些像“Monday March 21st 2012”这样的字符串时,它总是匹配两个子字符串:Mo'nd'ay March '21st' 2012。
所以我很困惑为什么它匹配“Mo'nd'ay”?
因为您缺少一组括号。尝试:
((\d{1,2})((st)|(nd)|(rd)|(th)))
你有什么,匹配:
(\d{1,2})(st) OR (nd) OR (rd) OR (th)
您的 s 周围没有正确的括号|。你有((\d{1,2})(st)|(nd)|(rd)|(th)),但你应该有:(\d{1,2})(st|nd|rd|th)。
|
((\d{1,2})(st)|(nd)|(rd)|(th))
(\d{1,2})(st|nd|rd|th)
您正在匹配字符串nd、rd、th或 (后跟一个或两个数字st)。
nd
rd
th
st