简体   繁体   English

正则表达式特殊字符

[英]Regular Expression special characters

I have this code with this regular expression: 我有此正则表达式的代码:

string lPattern = "\\S*[a-zA-Z]+\\S*";

System.Text.RegularExpressions.Regex lRegex =
    new System.Text.RegularExpressions.Regex(lPattern);

if (!lRegex.IsMatch(phone))
{
    return true;
}

I want to make it so if space x space " x " is in the string is will also return true. 我想这样做,如果字符串中的空格x空间“ x”也将返回true。 How would I change this regular expression to include this? 我将如何更改此正则表达式以包括此内容? Thanks! 谢谢!

" x |\\S*[a-zA-Z]+\\S*"

还是我错过了什么?

Apparently, this code is trying to validate a phone number by matching any alpha characters in the phone number -- if a match is made, then the phone number is invalid. 显然,此代码试图通过匹配电话号码中的任何字母字符来验证电话号码-如果匹配,则电话号码无效。

So you want any alpha character (optionally embedded inside non-whitespace characters, though these are listed as optional, so they're actually irrelevant) except for the sequence x (space x space) to match and trigger failure. 因此,除了序列x (空格x空格)可以匹配并触发失败之外,您需要任何字母字符(可选地嵌入在非空白字符中,尽管这些字符被列为可选的,因此它们实际上是不相关的)。

This sounds like you want accept the case of people embedding an extension into the phone number, as in: 123-123-1234 x 789 这听起来像您要接受人们将分机嵌入电话号码的情况,例如: 123-123-1234 x 789

.*([a-zA-Z])+(?<! x).*

comes very close. 非常接近。

EDIT: This uses a negative look-behind construction. 编辑:这使用了负向后看结构。 (?<! allows the regex to keep going only if from the current match position, the previous characters do not match what's inside the remainder of the parentheses. So, if there was a leading x it would fail the match. (?<!仅当从当前匹配位置开始,前一个字符与其余括号内的字符不匹配时,才允许正则表达式继续运行。因此,如果前导x会导致匹配失败。

-- -

Using this regex, the following don't match: 使用此正则表达式时,以下内容不匹配:

  • 123-123-1234 123-123-1234
  • 123-123-1234 x789 123-123-1234 x789
  • 123-123-1234 x 789 123-123-1234 x 789

But it does match: 但它确实匹配:

  • 123-123a-1234 123-123a-1234
  • 123-123 asdfasdf 12312 123-123 asdfasdf 12312
  • 123-123x-123123 123-123x-123123
  • 123-123 xfasdf 123-123 xfasdf

And the ones that match are invalid , by virtue of the ! 而匹配的项就是无效! in your if-statement. 在您的if陈述中。

But having said that, your code would be much easier if you wrote the phone number validation as a positive validation (ie what do you allow), not a negative invalidation (what do you specifically avoid). 但是话虽如此,如果您将电话号码验证写为肯定验证(即您允许的内容)而不是否定无效(您特别避免什么),则代码会容易得多。 By writing what you allow, you'll be able to prevent edge cases much easier. 通过编写允许的内容,您可以更轻松地防止出现极端情况。

For example, the following is not matched by both regex's: 例如,以下两个正则表达式不匹配:

  • 123-###-1234 123 - ### - 1234
  • 123-!$#&^(##^#*@#(!@&#(#-1234 123 - !$#&^(## ^#* @#(@&#(# - 1234

and this would return true from your function. 这将从您的函数返回true。 Is that what you really want? 那是你真正想要的吗?

I strongly suggest positive validation, not negative invalidation. 我强烈建议正面验证,而不是负面无效。


EDIT: @Aprillion suggests this previous linked question: A comprehensive regex for phone number validation 编辑:@Aprillion建议这个以前的链接的问题: 电话号码验证的综合正则表达式

This shows how complicated phone number validation can be (especially if you're validating for more than 1 country). 这显示了电话号码验证的复杂程度(特别是如果您要验证多个国家/地区的情况)。 There's a lot of good suggestions in there, though it's not be the be-all-end-all summary. 尽管这里并不是万能的摘要,但其中有很多好的建议。 My inclination would be on the front-end to allow only digits (w/ an optional free-from comment field) for phone number entry. 我的倾向是在前端,仅允许输入数字(带有可选的免提注释字段)来输入电话号码。

I must admit also, that, it does give me a little less confidence on positive validation -- especially if you have to deal w/ an arbitrary international #. 我还必须承认,这确实使我对肯定验证的信心降低了- 尤其是当您必须处理任意国际编号时。

A lot would be dependent upon the OP's real requirements. 很大程度上取决于OP的实际要求。

string lPattern = @"^\s*[a-zA-Z]+\s*$";

// Start of string
// Zero or more white space
// Letters any case 1 or more
// Zero or more white space
// End of string

\S means anything other than whitespace
\s is to match for whitespace

If you want to validate a phone number, I would also recommend turning the logic around - ie try to find a valid phone number rather than invalid. 如果您想验证电话号码,我也建议您改变逻辑-即尝试查找有效的电话号码而不是无效的电话号码。 It's easier to restrict what you want to match than to think of all possibilities that you want to reject. 限制要匹配的内容比考虑要拒绝的所有可能性要容易得多。

I would use this regex: 我将使用此正则表达式:

(\d+(?:-|\s)?)+(?:\s*x\s*\d+)?

And the code would become: 代码将变成:

string lPattern = @"(\d+(?:-|\s)?)+(?:\s*x\s*\d+)?";

        System.Text.RegularExpressions.Regex lRegex = new System.Text.RegularExpressions.Regex(lPattern);

        if (lRegex.IsMatch(phone)) return true;

If that's not what you are after then you'll have to provide some context. 如果那不是您想要的,那么您将必须提供一些上下文。

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

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