简体   繁体   English

正则表达式部分匹配

[英]Partial match with Regular Expression

Is there a way to determine that a single char is valid when a regular expression expects a specific number of that char? 当正则表达式需要特定数量的字符时,是否可以确定单个字符有效?

I have a WPF custom keyboard and would like to adjust each key's availability based on a regular expression. 我有WPF自定义键盘,并且想根据正则表达式调整每个键的可用性。 This will work well when the expression is fairly simple and does not expect specific order of the chars or a specific length to satisfy the pattern. 当表达式相当简单并且不期望字符的特定顺序或特定长度满足模式时,这将很好地工作。

However, when the pattern becomes more complex and specific, testing a single char against it will always fail. 但是,当模式变得更加复杂和具体时,针对它测试单个字符将始终失败。

For instance, given the regular expression [a-zA-Z0-9]{4} 例如,给定正则表达式[a-zA-Z0-9]{4}

These values will succeed: 这些值将成功:

  • ABCD A B C D
  • abcd A B C D
  • 1234 1234
  • A23e A23e

The expression clearly expects alphanumerical chars only. 该表达式显然只需要字母数字字符。 I would like a method that given the expression will reject special char, say "%", but accept "a" as "a" is acceptable in [a-zA-Z0-9] . 我想要一个给定表达式的方法,该方法将拒绝特殊字符,例如“%”,但在[a-zA-Z0-9]接受“ a”作为“ a”是可以接受的。 The only issue is the specific length that will not be satisfied. 唯一的问题是无法满足的特定长度。

I am currently using Regex.IsMatch . 我目前正在使用Regex.IsMatch I guess I am looking for a partial match testing method. 我想我正在寻找部分比赛测试方法。

Sure, you can, but not using the built-in regex engine unfortunately. 当然可以,但是不幸的是您不能使用内置的正则表达式引擎。 You can use PCRE instead, which provides the partial matching feature you're asking for. 您可以改用PCRE ,它提供了您要求的部分匹配功能。

From the PCRE docs: 从PCRE文档中:

In normal use of PCRE, if the subject string that is passed to a matching function matches as far as it goes, but is too short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. 在PCRE的正常使用中,如果传递给匹配函数的主题字符串尽可能地匹配,但太短而无法匹配整个模式,则返回PCRE_ERROR_NOMATCH There are circumstances where it might be helpful to distinguish this case from other cases in which there is no match. 在某些情况下,将这种情况与其他不匹配的情况区分开可能会有所帮助。

Consider, for example, an application where a human is required to type in data for a field with specific formatting requirements. 例如,考虑一个需要人为具有特定格式要求的字段输入数据的应用程序。 An example might be a date in the form ddmmmyy , defined by this pattern: 一个示例可能是ddmmmyy格式的日期,该日期由以下模式定义:

  ^\\d?\\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\d\\d$ 

If the application sees the user's keystrokes one by one, and can check that what has been typed so far is potentially valid, it is able to raise an error as soon as a mistake is made, by beeping and not reflecting the character that has been typed, for example. 如果应用程序一次又一次地看到用户的击键,并且可以检查到目前为止所键入的内容是否可能有效,则它会通过发出哔哔声而不反映已输入的字符来立即引发错误。例如键入。 This immediate feedback is likely to be a better user interface than a check that is delayed until the entire string has been entered. 与将检查延迟到输入整个字符串之前相比,这种即时反馈可能是更好的用户界面。 Partial matching can also be useful when the subject string is very long and is not all available at once. 当主题字符串很长并且不能一次全部使用时,部分匹配也很有用。

PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and PCRE_PARTIAL_HARD options, which can be set when calling any of the matching functions. PCRE通过PCRE_PARTIAL_SOFTPCRE_PARTIAL_HARD选项支持部分匹配,可以在调用任何匹配函数时设置这些选项。 For backwards compatibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT . 为了向后兼容, PCRE_PARTIAL为同义词PCRE_PARTIAL_SOFT The essential difference between the two options is whether or not a partial match is preferred to an alternative complete match, though the details differ between the two types of matching function. 两种选择之间的本质区别在于,尽管两种匹配功能的细节不同,但部分替代还是完全替代还是部分匹配更可取。 If both options are set, PCRE_PARTIAL_HARD takes precedence. 如果同时设置了两个选项,则PCRE_PARTIAL_HARD优先。


But PCRE is a C library... So I've built a PCRE wrapper for .NET . 但是PCRE是一个C库...因此,我为.NET构建了PCRE包装器

Usage example from the readme : 自述文件中的用法示例:

var regex = new PcreRegex(@"(?<=abc)123");
var match = regex.Match("xyzabc12", PcreMatchOptions.PartialSoft);
// result: match.IsPartialMatch == true

A little caution though: the wrapper is currently at v0.3, using PCRE v8.36 but PCRE v10.0 was released recently (with a new API), so expect some breaking changes in the API of v0.4 of PCRE.NET. 不过请注意一点:包装器当前使用的是PCRE v8.36,但版本为v0.3,但PCRE v10.0是最近发布的(带有新的API),因此请期待PCRE.NET v0.4 的API中的一些重大更改 The behavior should stay the same though. 行为应保持不变。

And also, you should be aware of the differences between .NET and PCRE regex flavors. 而且,您应该知道.NET和PCRE regex风格之间的区别 This should not be a problem for most cases though. 不过,在大多数情况下,这应该不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM