此正则表达式中单引号的目的是什么? [英] What is the purpose of the single quotes in this regex expression?

查看:57
本文介绍了此正则表达式中单引号的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下正则表达式继承了一些C#代码

I've inherited some C# code with the following regular expression

Regex(@"^[a-zA-Z''-'\s]{1,40}$")

除了单引号的作用之外,我了解此字符串.我到处搜索,但似乎找不到解释.有什么想法吗?

I understand this string except for the role of the single quotes. I've searched all over but can't seem to find an explanation. Any ideas?

推荐答案

据我所知,该表达式是多余的.

From what I can tell, the expression is redundant.

它匹配 az AZ ,或'字符,或'之间的任何字符'(当然,它又只是'字符或任何空格.

It matches a-z or A-Z, or the ' character, or anything between ' and ' (which of course is only the ' character again, or any whitespace.

我已经使用 RegexPal 对此进行了测试,除这些字符外,它似乎与其他任何字符都不匹配.也许该序列是由代码生成的,还是在较早的版本中用于匹配更大范围的字符?

I've tested this using RegexPal and it doesn't appear to match anything but these characters. Perhaps the sequence was generated by code, or it used to match a wider range of characters in an earlier version?

更新:从您的评论(与名字匹配)中,我将继续猜测作者认为他是在通过连字符转义用引号括起来,它并不是最出色的软件测试仪.他们可能的意思是:

UPDATE: From your comments (matching a name), I'm gonna go ahead and guess the author thought (s)he was escaping a hyphen by putting it in quotes, and wasn't the most stellar software tester. What they probably meant was:

Regex(@"^[a-zA-Z'\-\s]{1,40}$") //Escaped the hyphen

哪个也可以写成:

Regex(@"^[a-zA-Z'\s-]{1,40}$") //Put the hyphen at the end where it's not ambiguous

这篇关于此正则表达式中单引号的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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