Regex “匹配”;“神奇的”;带正则表达式的日期
我发现了一个与“神奇”日期匹配的正则表达式(其中一年的最后两位数与月份和日期的两位数相同,例如2008-08-08): 。。。但我不明白。它是如何工作的?您可以使用正则表达式:Regex “匹配”;“神奇的”;带正则表达式的日期,regex,date,datetime,date-formatting,Regex,Date,Datetime,Date Formatting,我发现了一个与“神奇”日期匹配的正则表达式(其中一年的最后两位数与月份和日期的两位数相同,例如2008-08-08): 。。。但我不明白。它是如何工作的?您可以使用正则表达式: \b[0-9]{2}([0-9]{2})-\1-\1\b 最后两位数字被捕获到捕获组#1中,然后被捕获组的反向引用(即,\1被用于以后的月份和日期部分) 下面是同样的正则表达式,用注释详细编写: \b # The beginning or end of a word. [0-9]
\b[0-9]{2}([0-9]{2})-\1-\1\b
最后两位数字被捕获到捕获组#1中,然后被捕获组的反向引用(即,\1
被用于以后的月份和日期部分)
下面是同样的正则表达式,用注释详细编写:
\b # The beginning or end of a word.
[0-9] # Any one of the characters '0'-'9'.
[0-9] # Any one of the characters '0'-'9'.
( # Save everything from here to the matching ')' in a variable '\1'.
[0-9] # Any one of the characters '0'-'9'.
[0-9] # Any one of the characters '0'-'9'.
) #
- # The literal character '-'
\1 # Whatever was saved earlier, between the parentheses.
- # The literal character '-'
\1 # Whatever was saved earlier, between the parentheses.
\b # The beginning or end of a word.
在“2008-08-08”的情况下,“20”与前两个[0-9]
s匹配,然后紧随其后的“08”与下两个[0-9]
s匹配(括号中,因此“08”保存到变量\1
)
然后匹配一个hypen,然后再匹配一次
08
(因为它在前面存储在变量\1
中),然后再匹配一个hypen,然后再匹配一次08
(如\1
)那么它对您有效吗?您不理解它,还是对您无效?您没有解释问题或提出问题。也许会有帮助。我想了解它,它确实对我有用。谢谢。如果你能解释得更详细一些,我会很高兴的。你能解释吗?请在回答中添加解释和演示。这是一个很棒的解释谢谢我现在收到了谢谢希望我能投票支持这个答案(我很高兴这有帮助……如果你愿意,你可以回答:-)
\b # The beginning or end of a word.
[0-9] # Any one of the characters '0'-'9'.
[0-9] # Any one of the characters '0'-'9'.
( # Save everything from here to the matching ')' in a variable '\1'.
[0-9] # Any one of the characters '0'-'9'.
[0-9] # Any one of the characters '0'-'9'.
) #
- # The literal character '-'
\1 # Whatever was saved earlier, between the parentheses.
- # The literal character '-'
\1 # Whatever was saved earlier, between the parentheses.
\b # The beginning or end of a word.