简体   繁体   English

正则表达式不过滤特殊符号

[英]Regex expression not filtering special symbols

I'm currently using the following line of code: 我当前正在使用以下代码行:

Regex Regex_Alpha = new Regex(@"[a-zA-Z]+('[a-zA-Z])?[a-zA-Z]*");

What I want to do is filter the input of text fields with the condition that input should only be letters and the apostrophe symbol (actually, I still want to add more, but I'm trying to resolve this first). 我想做的是过滤文本字段的输入,条件是输入只能是字母和撇号(实际上,我仍然想添加更多,但是我想首先解决这个问题)。

Right now, it is accepting ALL characters, even numbers. 现在,它接受所有字符,甚至数字。

With my understanding of Regex, I tried to formulate my own expression in the line of: 基于对Regex的理解,我尝试根据以下方面来表达自己的表达方式:

Regex Regex_Alpha = new Regex(@"^[a-zA-Z'-"+$);

It filters numbers, but doesn't accept the apostrophe symbol. 它过滤数字,但不接受撇号符号。 Tried to remove the @ sign and filter the apostrophe with the backslash escape character, but still no use. 试图删除@符号并使用反斜杠转义字符过滤撇号,但仍然没有用。

What should be the best approach to filter the input so that it only accepts letters and apostrophe? 筛选输入以使其仅接受字母和撇号的最佳方法应该是什么? (I'll do the rest of the symbols once I understand how this one should work) (一旦我理解了该符号的工作原理,我将处理其余符号)

As I've commented, your first regular expression is a pretty good shot at "letters, with a single apostrophe not at either end". 正如我已经评论过的那样,您的第一个正则表达式是“字母,两端没有单撇号”的很好的表达。 However, it matchs any string with even a single letter because a regular expression looks for any match in the input, not for whether the entire input matches. 但是,它可以将任何字符串与单个字母匹配,因为正则表达式会在输入中查找任何匹配项,而不是整个输入是否匹配。

You can fix this by doing what you've done in your second regular expression - just put a ^ at the start and a $ at the end. 您可以通过在第二个正则表达式中执行的操作来解决此问题-只需将^放在开头,将$放在结尾。 This means the start and end of the expression have to match the start and end of the input, so it ensures the whole input is only made up of letters and a possible apostrophe. 这意味着表达式的开始和结束必须与输入的开始和结束相匹配,因此可以确保整个输入仅由字母和可能的撇号组成。

Regarding your second regular expression, you have a few of problems. 关于第二个正则表达式,您有一些问题。

  • If you want a double-quote in a @"..." string literal, you need to put two double quotes. 如果要在@"..."字符串文字中使用双引号,则需要放置两个双引号。 (I think this might just be a typing mistake in your question, as what you currently have wouldn't even compile.) (我认为这可能只是您问题中的一个打字错误,因为您目前所拥有的甚至无法编译。)
  • You need to close your character class with a ] , otherwise the [ and everything inside just get treated as a sequence of characters to match, one after the other. 您需要使用[ ]关闭字符类,否则[和其中的所有内容都将被视为要匹配的字符序列,一个接一个。
  • If you want a hyphen in a character class, it has to go at the start or end, or it gets mistaken for a "between" hyphen (as in AZ ). 如果要在字符类中使用连字符,则必须在开头或结尾使用连字符,否则会误将其视为“之间”连字符(如AZ )。

The expression @"^[a-zA-Z'""-]+$" should match "any string entirely made of letters, apostrophes, quotes or hyphens". 表达式@"^[a-zA-Z'""-]+$"应该匹配“完全由字母,撇号,引号或连字符组成的任何字符串”。

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

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