REGEX - match a special character anywhere in the string

I am not a regular expression guy, so I need a little help, I just can’t get around this topic ...

I am writing a registration page and using asp.net validators. I need a regular expression validator to match a special character anywhere in the word, so it will satisfy the requirement that says "ID must have at least one special character." Allowed characters:! "# $% and '() * +, -, / :; <=>? @ [\] ^ _` {|} ~

Once again, I'm the youngest, and I have problems wrapping my head around this stuff at this point ...

At the moment I have

[\[\]\^\$\.\|\?\*\+\(\)\\~`!@#%&-_+={}'""<>:;, ]{1,}

but for some reason it also matches numbers and sometimes does not match the characters at the beginning of a word ...

So please, if anyone can help me with this ...

+5
source share
5 answers

You need to escape '-' and '!'

[\[\]\^\$\.\|\?\*\+\(\)\\~`\!@#%&\-_+={}'""<>:;, ]{1,}
+4
source

It would be easier to do the opposite

^[0-9]|[a-z]|[A-Z]$

it has a special character if it does not match

+2
source

asp.net . , []. - . , , ([-aa] [aa-]) . , "" , , ([^not me]).

, , (.NET , ). , .

asp.net . relavent, . , , , .

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="TextBox1" ErrorMessage="Input is not valid." 
        ValidationExpression="^.*[\[\]^$.|?*+()\\~`!@#%&\-_+={}'&quot;&lt;&gt;:;,\ ].*$"></asp:RegularExpressionValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />

, , , . <+'alpha char', "" , .

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="TextBox1" ErrorMessage="Input is not valid." 
        ValidationExpression="^(?!.*&lt;(?:/|[A-Za-z_:])).*[\[\]^$.|?*+()\\~`!@#%&\-_+={}'&quot;&lt;&gt;:;,\ ].*$"></asp:RegularExpressionValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />

, .

+1

You can crop this a bit and match character ranges. For example, this matches all special (and printed) characters (including unicode):

[^\p{L}]

I suggest that if you need to match 1 or more, which is typical for your case, then this will work too (note that the regex operator +is a shorthand for{1,}

[^\p{L}]+
0
source

Here you can learn more about regex: http://regex.learncodethehardway.org/

it is not so difficult to do;

good luck

-4
source

All Articles