Page 1 of 1

Need a Regex

Posted: Thu Mar 27, 2008 12:25 pm
by bvsuresh_reddy
Hi I need a regex which should match the following criteria

1. input string should not contain < followed by >
2. input string should not contain white spaces.

So the regular expression should return false, if the input string contains <something> pattern or spaces.

Ex:
!@~#$%^&*()_+ - true
<dsf adfas df> - false
sdf<sdsdf>sdf - false
dfs<sdf sdf > -false
asdf adf saf - false
sdafasf - true
<sd^&@*#)@) - true
<*&&*(_)#(&$)> - false
&$*(&<DS(^(*>SD^(+ - false

Thanks

Re: Need a Regex

Posted: Fri Apr 04, 2008 6:23 am
by prometheuzz
If it matches ".*(<[^>]*>|\s).*" it's invalid. In Java:

Code: Select all

 
        String[] tests = {
            "!@~#$%^&*()_+",        // - true
            "<dsf adfas df>",       // - false
            "sdf<sdsdf>sdf",        // - false
            "dfs<sdf sdf >",        // - false
            "asdf adf saf",         // - false
            "sdafasf",              // - true
            "<sd^&@*#)@)",          // - true
            "<*&&*(_)#(&$)>",       // - false
            "&$*(&<DS(^(*>SD^(+",   // - false
        };
        
        for(String t: tests) {
            System.out.println(!t.matches(".*(<[^>]*>|\\s).*")+"\t\t"+t);
        }