Java的Matcher.matches()和Matcher.lookingAt()的C#/.NET等效项 [英] C#/.NET equivalent for Java's Matcher.matches() and Matcher.lookingAt()

查看:42
本文介绍了Java的Matcher.matches()和Matcher.lookingAt()的C#/.NET等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来确定单个正则表达式与C#中的整个字符串,字符串开头还是部分字符串匹配.

I'm looking for a way to decide if a single regular expression matches a whole string, start of the string, or a part of it in C#.

在Java中,您可以构造一个 Matcher 对象,并使用方法 matches()来检查它是否与整个输入匹配, lookingAt()来查看它是否与输入的开头匹配,而 find()来查看在串中是否完全匹配.

In Java, you can construct a Matcher object, and use the methods matches() to check if it is a match for the whole input, lookingAt() to see if it matches the start of the input, and find() to see if there is any match from within the strung at all.

在.NET中,我可以使用 Match.Success 来查看是否存在匹配项,并检查 Match.Index Match.Length 对于上述条件.

In .NET, I can use Match.Success to see if there is any match, and check Match.Index and Match.Length for the above conditions.

问题是,如果找到较小的匹配项,它不会尝试匹配整个输入.例如,如果输入"1234"出现在正则表达式 @"\ d {2} | \ d {4}" 中,则它将匹配前两位.

The problem is, it does not try to match the whole input if a smaller match is found. For example, if the input "1234" is presented to the Regex @"\d{2}|\d{4}", it will match the first two digits.

在这种情况下,我需要使用模式 @"^(\ d {2} | \ d {4})$" 构造另一个 Regex 测试整个输入,然后测试另一个输入是否与开头匹配.

In such a case, I need to have another Regex constructed with the pattern @"^(\d{2}|\d{4})$" to test for the whole input, and then test the other one to see if it matches the beginning.

是否没有办法告诉同一个 Regex 类更喜欢全输入匹配,因此出于这个原因,我不需要创建(和编译)另一个Regex?

Isn't there a way to tell the same Regex class to prefer whole-input matches, so that I won't need to create (and compile) another Regex for this reason?

问题不在于模式,它只是一个例子,用以说明我的意思.

The question is not about the pattern, it is just an example to show what I mean.

推荐答案

不,.NET中没有仅匹配开头"或仅匹配整个字符串"正则表达式方法.您确实需要使用锚来构造这种行为.

No, there is no "only match at the beginning" or "only match the entire string" regex method in .NET. You do need to construct this behaviour using anchors.

在某种程度上,我比Java更喜欢此功能,原因有二:

In a way, I much prefer this over what Java does for two reasons:

  • 很多人对 .matches()感到困惑,因为他们不知道它必须匹配整个字符串.关于这个确切的问题,我至少看到了十几个问题.
  • 为什么要在两个不同的地方冗余地保留相同的功能?您已经在正则表达式语法中有了锚,那么为什么要在某些(有时是误导性的)方法中使它们隐式?(我正在与您交谈,Python,以及您的 re.search() re.match() * ).
  • Many people are confused by .matches() because they don't know it has to match the entire string. I've seen at least a dozen questions on SO about this exact problem.
  • Why keep the same functionality redundantly in two different places? You have anchors in regex grammars already, so why make them implicit in some (sometimes misleadingly-named) methods? (I'm talking to you, Python, and your re.search() vs. re.match()*).

也就是说,这也很容易做到:

That said, it's also very easy to do:

  • @"\ A(?:" +原始正则表达式+ )" -> lookingAt()(现在谁想到了那个名字)?
  • @"\ A(?:" +原始正则表达式+ @)\ z" -> matches()
  • @"\A(?:" + original regex + ")" --> lookingAt() (now who thought of that name)?
  • @"\A(?:" + original regex + @")\z" --> matches()

* re.match()的行为类似于Java的 lookingAt(),而不是 matches(),如果您想知道的话.

* re.match() behaves like Java's lookingAt(), not like matches(), in case you were wondering.

这篇关于Java的Matcher.matches()和Matcher.lookingAt()的C#/.NET等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆