如何将撇号添加为现有正则表达式的允许字符?

时间:2020-12-27 22:25:16

I have the following regular expression for one of my name fields in C# web app:

我在C#web app中为我的一个名字字段提供了以下正则表达式:

^[A-Za-zÀ-ſ0-9.,#&-/'_!@;]?[a-zA-ZÀ-ſ0-9 '#&-/.,_:!@;]*[A-Za-zÀ-ſ0-9.,#-/_!@;]$

How can I properly modify it to add apostrophe/single quote character (') as an allowed character to it?

如何正确修改它以将撇号/单引号字符(')添加为允许字符?

2 个解决方案

#1


1  

' is used for declaring a char, so put a backslash in front of the ' to escape it, like this \'.

'用于声明一个字符,所以在'之前加一个反斜杠来逃避它,就像这个'。

#2


0  

It turned out that the RegEx has been fine, and it was the way the data has been input into database that caused the problem. Insert statements should have the apostrophes escaped. Even though the apostrophes were getting displayed correctly, they had been failing the RegEx check due to lack of escaping apostrophes. Thanks for your advice and sorry in case of any disapointment!

事实证明,RegEx一直很好,而且这是数据输入数据库的方式导致问题。插入语句应该对撇号进行转义。即使撇号正确显示,由于缺少转义撇号,它们也未能通过RegEx检查。感谢您的建议,如有任何失望,请抱歉!

#1


1  

' is used for declaring a char, so put a backslash in front of the ' to escape it, like this \'.

'用于声明一个字符,所以在'之前加一个反斜杠来逃避它,就像这个'。

#2


0  

It turned out that the RegEx has been fine, and it was the way the data has been input into database that caused the problem. Insert statements should have the apostrophes escaped. Even though the apostrophes were getting displayed correctly, they had been failing the RegEx check due to lack of escaping apostrophes. Thanks for your advice and sorry in case of any disapointment!

事实证明,RegEx一直很好,而且这是数据输入数据库的方式导致问题。插入语句应该对撇号进行转义。即使撇号正确显示,由于缺少转义撇号,它们也未能通过RegEx检查。感谢您的建议,如有任何失望,请抱歉!