简体   繁体   English

使用RegEx排除某些字符

[英]Exclude certain characters using RegEx

Try as I might, I can't get a RegEx to exclude space or single quotes. 尽我所能,我无法获得RegEx来排除空格或单引号。

  • The string "abc" is allowed 允许字符串“abc”
  • Not allowed: "a'bc", "'", "'abc", "'''", "abc''" etc 不允许:“a'bc”,“'”,“'abc”,“'''”,“abc”等
  • Spaces could replace the ' too in the above example 在上面的例子中,空格也可以取代'
  • Trailing and leading spaces are assumed to be removed already 假设已经删除了尾随和前导空格
  • Empty strings are checked elsewhere 其他地方检查空字符串
  • Target language is javascript 目标语言是javascript

I'd use PATINDEX if I was in SQL. 如果我在SQL中,我会使用PATINDEX。

Or NOT a positive match on either space or single quote, if I could negate... 如果我可以否定的话,或者不是空格或单引号的正面匹配......

I've tried (for single quote only) 我试过了(仅限单引号)

  • \\w*[^']\\w*
  • ^\\w*[^']\\w*$
  • others I forget now 别人,我现在忘了

Please put me out of my misery so I can sleep tonight. 请把我从痛苦中解救出来让我今晚能入睡。

Edit: 编辑:

  • Target string will not be surrounded by Quotes. 目标字符串不会被引号包围。 I thought thy might add clarity 我以为你可能会增加清晰度
  • If "Target language is javascript" is wrong, then it's c#. 如果“目标语言是javascript”是错误的,那么它就是c#。 I'd have to check where we do the validation exactly: client javascript or server c# 我必须确切地检查我们在哪里进行验证:客户端javascript或服务器c#

^[^\'\ ]*$
?

Quite simple. 非常简单。 Does not allow empty strings. 不允许空字符串。

^[^' ]+$

i think this 我认为这

^\w*$

should work as \\w does not include single quote or space. 应该工作为\\w不包括单引号或空格。

没有阅读细节,我在那里看不到[^'](有空格和单引号)。

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

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