www.webdeveloper.com
Results 1 to 10 of 10

Thread: string matches

  1. #1
    Join Date
    Jan 2003
    Location
    PA
    Posts
    1,898

    string matches

    I have a userid field that I only want to allow "a-zA-Z0-9". We are using jdsk 1.3.1, not 1.4, so we can't use import java.util.regex. How can I accomplish this?

    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Setup an array or comma-delimited list of illegal characters and then search the string for them.
    Code:
    public boolean isValid(String strUser)
    {
       String strIllegal = "*,&,%,(,),#,@";
       String [] arrIllegal = strIllegal.split(',');
    
       for (int i=0; i<arrIllegal.length; i++)
          if (strUser.indexOf(arrIllegal[i]) > -1)
             return false;
       return true;
    }
    That *should* return true if the string does not contain any of the defined illegal characters.

    Regards.

  3. #3
    Join Date
    Jan 2003
    Location
    PA
    Posts
    1,898
    Ok, I am able now to use jave.util.regex for this. The below code runs, but it asways comes back as "no match", meaning it didn't evaluate the string properly, because it should have returned true. Could someone help please.

    Code:
    	public static void main(String[] args) {
    		
    		String test = "ef(123"; //this should fail because of the (, but it always passes, and prints out no match below
    		
    		Pattern p = Pattern.compile("\\w", Pattern.CASE_INSENSITIVE);
    		Matcher m = p.matcher(test);
    		if(m.matches()==true){
    			System.out.println("match true");
    		} else {
    			System.out.println("no match");
    		}
    		
    	}

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    String test = "ef(123"; //this should fail because of the (
    I think this assumption is false.

    Try Pattern.matches(".+\\w.+","ef(123")

    BTW, I recommend anyone learning Java go to http://beanshell.org for a great learning tool.
    Last edited by ray326; 11-18-2004 at 02:26 PM.

  5. #5
    Join Date
    Jan 2003
    Location
    PA
    Posts
    1,898
    Here is an updated code

    Code:
    String test = "jas&on";
    		
    		Pattern p = Pattern.compile("\\w", Pattern.CASE_INSENSITIVE);
    		Matcher m = p.matcher(test);
    		if(m.matches()==false){
    			System.out.println("invalid string");
    		} else {
    			System.out.println("valid string");
    		}
    		
    	}
    For this example, I get "invalid string".

    If I replace "jas&on" with "jason", I still get "invalid string"???

  6. #6
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Is there something wrong with Java's Regex?

    e.g.,

    Code:
    boolean bad = Pattern.matches("\\W", "jason");
    if (bad)
    	System.out.println("Invalid");
    else
    	System.out.println("Valid");
    and

    Code:
    boolean bad = Pattern.matches("\\W", "!j@a#s$o%n^");
    if (bad)
    	System.out.println("Invalid");
    else
    	System.out.println("Valid");
    Both say Valid. The second absolutely should not.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  7. #7
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Well all regex implementations are not created equal but I'm not sure whether this is absolutely wrong. It certainly doesn't match the same way the Javascript or VisualREGEXP regex do.

  8. #8
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Having given the documentation a quick read, it seems that the matches(String regex, String input) method returns true if and only if the entire string matches the pattern. In otherwords, for the regex in your example to return true, every character would have to be a non-word character. Since that's not true in either case, bad is always set to false. The find() method of the matcher returns true if there is an occurrence of the pattern in the string:
    Code:
    Pattern p=Pattern.compile("\\W");
    boolean bad=p.matcher("jason").find(); //false
    System.out.println(bad);
    bad=p.matcher("jason$$").find(); //true
    System.out.println(bad);

  9. #9
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Good catch HaganeNoKokoro.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  10. #10
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Originally posted by HaganeNoKokoro
    Having given the documentation a quick read, it seems that the matches(String regex, String input) method returns true if and only if the entire string matches the pattern. In otherwords, for the regex in your example to return true, every character would have to be a non-word character. Since that's not true in either case, bad is always set to false. The find() method of the matcher returns true if there is an occurrence of the pattern in the string:
    Code:
    Pattern p=Pattern.compile("\\W");
    boolean bad=p.matcher("jason").find(); //false
    System.out.println(bad);
    bad=p.matcher("jason$$").find(); //true
    System.out.println(bad);
    Hence the reason for the pattern I used in my reply above. Still this is a good illustration that there is not really a "standard" for regex matchers even though the patterns look very very similar.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles