正则表达式的意图 [''-'] [英] Intention of regular expression [''-']

查看:22
本文介绍了正则表达式的意图 [''-']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过一个正则表达式 @"^[AZ]+[a-zA-Z''-'\s]*$" 用于检查字符串是否是人名并具有

<小时>

范围从 " 到 '

因此,如果您有一个带有 ["-'] 的范围,您将接受以下字符:"#$%&'

<小时>

范围从'到'(文字')

同样,如果你使用['-'],你使用的是从''的范围,这与使用文字字符串 ':

<小时>

连字符作为字符类中的文字

另一方面,如果您将连字符放在字符类的末尾,它将无法用作范围,而是使用文字字符串(如果您使用反斜杠 \-:

<小时>

单引号的冗余模式:[''-']

作为对您发布的特定模式的附加评论,您可以在屏幕截图中看到解释,简单地说它是简单匹配文字字符串 ' 的冗余模式.

I have seen a regular expression @"^[A-Z]+[a-zA-Z''-'\s]*$" for checking whether a string is person's name and had the same question described in Regular Expression format meaning of [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")] [duplicate]. Namely, what's the intention of [''-']?

Since all [''-'], ['] and ['-'] match single apostrophe, ['] should be enough and ['-'] looks redundant. However, I have seen this expression in several articles in MSDN and in books and so it seems a common pattern. I think there should be a legitimate reason to include ['-']. The comments to above question doesn't go into this point.

Added

Just for reference, I add the articles in Microsoft that use this expression. '' before hyphen is two consecutive single quotes but not double quotes.

解决方案

The hyphen in a regex character class has two meanings: a range between two characters like the classic [a-z] and simply the literal hyphen.

For the specific regex you posted [''-'] I think you mean ["-'], what you can find the explanation below. However, the pattern [''-'] it is a really ugly regex to just define the literal string ' (or it is simply a wrong regex).

However, usually people use hyphen with the most common ranges [A-Z], or [a-z] or [0-9], but there is a deeper behavior behind this. The ranges are applied using the ASCII table:


Range from " to '

So, if you have a range with ["-'] you will accept the characters: "#$%&'

Regex demo


Range from ' to ' (literal ')

Likewise, if you use ['-'], you are using a range from ' to ', which is the same of using the literal string ':

Regex demo


Hyphen as literal in character class

On the other hand, if you put the hyphen at the end of the character class it won't work as a range and will be used a literal string (same if you escape it with backslash \-:

Regex demo


The redundant pattern for a single quote: [''-']

As an additional comment for the specific pattern you posted, you can see the explanation in the screenshoot, what briefly it is a redundant pattern for simple match the literal string '.

Regex demo

这篇关于正则表达式的意图 [''-']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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