Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果只重复一组符号,如何使正则表达式匹配?_C#_Java_Regex - Fatal编程技术网

C# 如果只重复一组符号,如何使正则表达式匹配?

C# 如果只重复一组符号,如何使正则表达式匹配?,c#,java,regex,C#,Java,Regex,我需要一个可以匹配日期的正则表达式。但是,日、月和年的分隔符可能不同。它可能是一个点,一个破折号,一条斜线 我所拥有的是 ^([012]?[1-9]|3[01])[\\.\\-\\/\\\\](0?[1-9]|1[012])[\\.\\-\\/\\\\](19|20)\\d{2}$ 所以是 ([012]?[1-9]|3[01]) --the day part (0?[1-9]|1[012]) --the month part (19|20)\\d{2} --the year

我需要一个可以匹配日期的正则表达式。但是,日、月和年的分隔符可能不同。它可能是一个点,一个破折号,一条斜线

我所拥有的是

^([012]?[1-9]|3[01])[\\.\\-\\/\\\\](0?[1-9]|1[012])[\\.\\-\\/\\\\](19|20)\\d{2}$
所以是

([012]?[1-9]|3[01]) --the day part
(0?[1-9]|1[012])    --the month part
(19|20)\\d{2}       --the year part
分隔符重复了两次,根据我当前的表达式
[\\.\\-\\/\\\\\\\\]
,可能不同…我的意思是,它匹配,比如:

01.01-1986
虽然我希望它只在有两个点、两个破折号或两个定界符部分允许的任何东西时匹配…所以上面给出的示例不应该匹配

我想这可以通过正则表达式的分组模式来实现。但我不知道如何应用这个。而且我发现自己完全不知道如何用谷歌搜索这个

有人能把我推向正确的方向吗


附言:我最近看了……就我而言,我知道它会在没有它的几个月内与第31个相匹配,并且在2月份缺席的所有日子……一切都很好


在Java中,我目前使用以下代码:

String value = "31/12/2086";
String pattern = ...
boolean result = value.matches(pattern);
在C中#


如果有一种方法可以实现我想要的功能,那么如果该解决方案可以应用于这两种语言,那就太好了。

您可以在正则表达式中使用
\\x
,其中
x
是组号。它表示由组
x
匹配的相同字符串。所以在你的情况下,你可以使用

someRegex([.\\-/\\\\])someOtherRegex\\1anotherRegex
          ^^^^^^^^^^^               ^^^
            group 1                 here should appear same string as in group 1
范例

String regex = "someRegex([.\\-/\\\\])someOtherRegex\\1anotherRegex";
System.out.println("someRegex.someOtherRegex.anotherRegex".matches(regex));
System.out.println("someRegex.someOtherRegex-anotherRegex".matches(regex));
System.out.println("someRegex-someOtherRegex-anotherRegex".matches(regex));
输出:

true
false
true

非常感谢。这正是我所需要的。当你接受我的回答时,我也是这么想的:)无论如何,我很高兴你喜欢它
true
false
true