Results 1 to 8 of 8

Thread: regex problem

  1. #1
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default regex problem

    I have this class Regex and I execute it with the arguments

    [jJlL] "I'm a Java Lover"
    The code in the while-loop works well but the last System.out.println(Pattern.matches(args[0], args[1]) print always false. I can't figure out what's going wrong. Any help?

    Java Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Regex {
    	public static void main(String[] args) {
    		if (args.length != 2) {
    			System.out.println(args.length);
    			System.err
    					.println("usage: Regex \"regex expression\" \"source data\"");
    			System.exit(1);
    		} else {
    			Pattern p = Pattern.compile(args[0]);
    			Matcher m = p.matcher(args[1]);
    
    			System.out.println("regex is " + m.pattern()
    					+ " and source data is " + args[1]);
    			while (m.find()) {
    				System.out.println("Found at starting pos " + m.start() + " "
    						+ m.group());
    			}
    			System.out.println(Pattern.matches(args[0], args[1]));
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: regex problem

    The Pattern.matches( ... ) method tries to match the entire String against the regular expression pattern, i.e. if the String is aaaaab and the regular expression is a* this method returns false, no matter the matching prefix.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default Re: regex problem

    So to get rid of this problem I should pass char by char ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: regex problem

    Quote Originally Posted by javaPower View Post
    So to get rid of this problem I should pass char by char ?
    I don't know what your problem is, but you can also change the regular expression (RE) to ^.*RE.*$ so that it always matches the entire String. Also, what is wrong with the other methods in the Matcher? (read the API documentation for that class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    javaPower is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default Re: regex problem

    Don't get excited, I got it! Thanks!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: regex problem

    You're welcome of course; it's weekend so I'm extremely lazy and unable to get excited about anything ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: regex problem

    If you want to do it this way it's cool, but you can do a string.matches(regex) call. Much simpler code, easier to read and most probably just as efficient. e.g:

    String myName = "chris";
    boolean stringMatches = myName.matches("c+");
    return stringMatches;

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: regex problem

    Quote Originally Posted by christopherx View Post
    If you want to do it this way it's cool, but you can do a string.matches(regex) call. Much simpler code, easier to read and most probably just as efficient. e.g:

    String myName = "chris";
    boolean stringMatches = myName.matches("c+");
    return stringMatches;
    From the API documentation of the String class:

    An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression

    Pattern.matches(regex, str)
    Reread this thread again; the OP doesn't want this behaviour.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to use regex and split to solve a problem
    By PeteClimbs in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 08:31 PM
  2. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 7
    Last Post: 05-18-2010, 02:39 AM
  3. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 0
    Last Post: 05-17-2010, 03:03 PM
  4. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 07:17 AM
  5. regex problem - allowing optional space
    By Norm in forum Advanced Java
    Replies: 26
    Last Post: 10-06-2008, 04:38 PM

Posting Permissions

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