开发者

What is the regular expression for "No quotes in a string"?

开发者 https://www.devze.com 2023-04-13 03:53 出处:网络
I am trying to write a regular expressi开发者_开发问答on that doesn\'t allow single or double quotes in a string (could be single line or multiline string). Based on my last question, I wrote like thi

I am trying to write a regular expressi开发者_开发问答on that doesn't allow single or double quotes in a string (could be single line or multiline string). Based on my last question, I wrote like this ^(?:(?!"|').)*$, but it is not working. Really appreciate if anybody could help me out here.


Just use a character class that excludes quotes:

^[^'"]*$

(Within the [] character class specifier, the ^ prefix inverts the specification, so [^'"] means any character that isn't a ' or ".)


Just use a regex that matches for quotes, and then negate the match result:

var regex = new Regex("\"|'");
bool noQuotes = !regex.IsMatch("My string without quotes");


Try this:

    string myStr = "foo'baa";
    bool HasQuotes = myStr.Contains("'") || myStr.Contains("\""); //faster solution , I think.
    bool HasQuotes2 = Regex.IsMatch(myStr, "['\"]");

                if (!HasQuotes)
                {
                    //not has quotes.. 
                }


This regular expression below, allows alphanumeric and all special characters except quotes(' and "")

@"^[a-zA-Z-0-9~+:;,/#&_@*%$!()\[\] ]*$"

You can use it like

[RegularExpression(@"^[a-zA-Z-0-9~+:;,/#&_@*%$!()**\[\]** ]*$", ErrorMessage = "Should not allow quotes")]

here use escape sequence() for []. Since its not showing in this post

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号