Click here to Skip to main content
15,886,807 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is i want to check that the string should contain char 'space to ~' (CHAR 32 TO 126) and all other strings are invalid
for eg 'INSERT BLASE KSEM2200HPM' is valid string and 'INSERT BLASÉ KSEM2200HPM' is a invalid string
i have tried the regular expression to solve this problem '([^ -~])*' but not geting the proper result
Posted
Updated 21-Oct-12 21:22pm
v2
Comments
Ambesha 22-Oct-12 3:23am    
how you tried please share the code
Menon Santosh 22-Oct-12 3:28am    
SELECT 'INSERT BLASE KSEM2200HPM' WHERE GName REGEXP '([^ -~])*'
i have tried this query in mysql, it should return false but it is returning true
Ambesha 22-Oct-12 3:35am    
please add MYSQL and remove C# from queston category if you want to validate your string in mySql.
Menon Santosh 22-Oct-12 3:36am    
if i get the solution in c# also it will also be welcome :)

i have solved it

C#
string strData1 = "'INSERT BLASÉ KSEM2200HPM";
            string strData = string.Empty;
            for (int i = 0; i < 256; i++)
            {
                if(!(i>=32 && i<= 126))
                strData += ((char)i).ToString();
            }
            int indexOf = strData1.IndexOfAny(strData.ToCharArray());
            if (indexOf == -1)            {

                MessageBox.Show("Valid chars");
            }
            else
            {
                MessageBox.Show(" contain invalid chars");
            }
 
Share this answer
 
Here's the regex if you want it, it matches the hex ascii character range:
bool m = Regex.IsMatch("INSERT BLASE", "^[\x20-\x7E]*$");
 
Share this answer
 
v2
Comments
Menon Santosh 22-Oct-12 5:36am    
good work
Hello

Here is another way to do it:

C#
string input = "INSERT BLASÉ KSEM2200HPM";

Match match = Regex.Match(input, "[^ -~]");

if (match.Success)
    Console.WriteLine("not ok.");
else
    Console.WriteLine("ok.");


Valery.
 
Share this answer
 
Comments
Menon Santosh 22-Oct-12 5:37am    
good work
Valery Possoz 22-Oct-12 6:11am    
thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900