正则表达式模式 - 允许字母数字、一堆特殊字符,但不允许特定的字符序列

发布于 2024-07-11 17:46:29 字数 504 浏览 9 评论 0原文

我有以下正则表达式:

(?!^[&#]*$)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$

所以允许 AZ、aZ、0-9 和这些特殊字符 '.,&@:?!()$#/\

如果以下内容我想不匹配在字符串中的任何位置都会按以下顺序遇到字符集:

&#

当我仅使用“&#”作为输入运行此正则表达式时,它与我的模式不匹配,我收到一个错误,很好。 当我使用 '.,&@:?!()$#/\ABC123 运行正则表达式时,它确实符合我的模式,没有错误。

但是,当我运行它时:

'.,&#@:?!()$#/\ABC123

它也不会出错。 我在检查 &# 序列时出错了。

谁能告诉我我做错了什么,我不擅长这些事情。

I have the following regex:

(?!^[&#]*$)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$

So allow A-Z, a-Z, 0-9, and these special chars '.,&@:?!()$#/\

I want to NOT match if the following set of chars is encountered anywhere in the string in this order:

&#

When I run this regex with just "&#" as input, it does not match my pattern, I get an error, great. When I run the regex with '.,&@:?!()$#/\ABC123 It does match my pattern, no errors.

However when I run it with:

'.,&#@:?!()$#/\ABC123

It does not error either. I'm doing something wrong with the check for the &# sequence.

Can someone tell me what I've done wrong, I'm not great with these things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧故 2024-07-18 17:46:29

借用匹配带引号的字符串的技术,从字符类中删除 &,添加 & not 的替代方案,后跟 #,并允许字符串可选地以 & 结尾:

<代码>^((?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&[^#])*&?)$

Borrowing a technique for matching quoted strings, remove & from your character class, add an alternative for & not followed by #, and allow the string to optionally end with &:

^((?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&[^#])*&?)$

自我难过 2024-07-18 17:46:29

我实际上会分两部分进行:

  1. 检查您允许的字符集。 为此,我将查找不允许的字符,如果存在匹配则返回 false。 这意味着我有一个很好的简单表达:
    [^A-Za-z0-9'\.&@:?!()$#^]
  2. 检查您的禁用子字符串。 由于它只是一个子字符串,我什至可能不会在该部分使用正则表达式。

你没有提到你的语言,但如果是 C#:

bool IsValid(string input)
{
    return !(   input.Contains("&#")  
               || Regex.IsMatch(@"[^A-Za-z0-9'\.&@:?!()$#^]", input) 
            );
}

I would actually do it in two parts:

  1. Check your allowed character set. To do this I would look for characters that are not allowed, and return false if there's a match. That means I have a nice simple expression:
    [^A-Za-z0-9'\.&@:?!()$#^]
  2. Check your banned substring. And since it is just a substring, I probably wouldn't even use a regex for that part.

You didn't mention your language, but if in C#:

bool IsValid(string input)
{
    return !(   input.Contains("&#")  
               || Regex.IsMatch(@"[^A-Za-z0-9'\.&@:?!()$#^]", input) 
            );
}
以可爱出名 2024-07-18 17:46:29

^((?!&#)[A-Za-z0-9-'.,&@:?!()$#/\\])*$

请注意最后一个 \被转义(加倍)
如果不在反引号中,SO 会自动将 \\ 转换为 \

^((?!&#)[A-Za-z0-9-'.,&@:?!()$#/\\])*$

note that the last \ is escaped (doubled)
SO automatically turns \\ into \ if not in backticks

沉鱼一梦 2024-07-18 17:46:29

假设 Perl 兼容的 RegExp

不匹配字符串 '&#':

<代码>(?![^&]*&#)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$

尽管您不需要括号,因为您正在匹配整个字符串。

Assuming Perl compatible RegExp

To not match on the string '&#':

(?![^&]*&#)^([A-Za-z0-9-'.,&@:?!()$#/\\]*)$

Although you don't need the parenthesis because you are matching the entire string.

痴骨ら 2024-07-18 17:46:29

仅供参考,虽然 Ben Blank 的正则表达式有效,但它比需要的更复杂。 我会这样做:

^(?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&(?!#))+$

因为我使用了否定的前瞻而不是否定的字符类,所以正则表达式不需要任何额外的帮助来匹配字符串末尾的&符号。

Just FYI, although Ben Blank's regex works, it's more complicated than it needs to be. I would do it like this:

^(?:[A-Za-z0-9-'.,@:?!()$#/\\]+|&(?!#))+$

Because I used a negative lookahead instead of a negated character class, the regex doesn't need any extra help to match an ampersand at the end of the string.

小草泠泠 2024-07-18 17:46:29

我建议在条件中使用两个正则表达式:

    if (string has sequence "&#")
      return false
    else
      return (string matches sequence "A-Za-z0-9-'.,&@:?!()$#/\")

我相信你的第二个“主”正则表达式

^([A-Za-z0-9-'.,&@:?!()$#/\])$"

有几个错误:

  • 它只会测试你的集合中的一个字符 正则
  • 表达式中的 \ 字符是一个标记指示下一个字符是某种字符“类”的一部分(例如 \n = 是换行符)。 字符序列 \] 实际上导致您的括号列表不被终止。

您可能最好使用

^[A-Za-z0-9-'.,&@:?!()$#/\\]+$

注意斜杠字符由双斜杠表示。

+ 字符表示至少一个被测试的字符必须与正则表达式匹配; 如果可以传递零长度字符串,请将 + 替换为 *

I'd recommend using two regular expressions in a conditional:

    if (string has sequence "&#")
      return false
    else
      return (string matches sequence "A-Za-z0-9-'.,&@:?!()$#/\")

I believe your second "main" regex of

^([A-Za-z0-9-'.,&@:?!()$#/\])$"

has several errors:

  • It will test only one character in your set
  • The \ character in regular expressions is a token indicating that the next character is part of some sort of "class" of characters (ex. \n = is the line feed character). The character sequence \] is actually causing your bracketed list not to be terminated.

You may be better off using

^[A-Za-z0-9-'.,&@:?!()$#/\\]+$

Note that the slash character is represented by a double-slash.

The + character indicates that at least one character being tested has to match the regex; if it is fine to pass a zero-length string, replace the + with a *.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文