C#字符串不应仅包含空格或任何特殊字符,除了..'; ::“; [英] C# string should not contain only white spaces or any special character except ,.';:"

查看:416
本文介绍了C#字符串不应仅包含空格或任何特殊字符,除了..'; ::“;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式模式来验证字符串不只包含空格(只包含多个空格)(例如:" .length = 4)并且不应该包含!@ $#%^& *()字符.

I need a regular expression pattern to verify string does not contain only white spaces(blank with multiple space only)(Ex: " ".length = 4) and should not contain !@$#%^&*() characters.

Regex regex =新的Regex(@. \ S +."); 这个检查空白.我需要在一个正则表达式模式中同时使用这两种条件.

Regex regex = new Regex(@".\S+."); This one checks for white spaces. I need both condition in one Regex pattern.

   Result
   ---------

  • ":错误
  • "ad af":正确
  • "asd asd":是
  • "asdf":是
  • "asdf @ df dsfs":false
  • #":假
    • " " : false
    • "ad af" : true
    • " asd asd " : true
    • " asdf " : true
    • "asdf@df dsfs " : false
    • " # " : false
    • 推荐答案

      作为单个正则表达式:

      !Regex.IsMatch(input, "^\s+$|[!@$#%^&*()]");
      

      这意味着:

      ^\s+$        //Is entirely composed of one or more whitespace characters 
      |            //OR
      [!@$#%^&*()] //Contains any one of the given special characters
      

      此正则表达式返回您想要的真相的对立面(即,它查找所有空格或包含特殊字符的任何内容),因此我们不使用!来满足您的要求

      This regex returns the opposite of the truth you want (i.e. it looks for anything that is all whitespace OR does contain a special char), so we NOT it with ! to match your requirements

      这篇关于C#字符串不应仅包含空格或任何特殊字符,除了..'; ::“;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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