Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Regular Expression Difficulties...

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Regular Expression Difficulties...

    Hi everyone!

    Right now I'm working on a regular expression that searches for comparison operators (">", "<=", "!=", etc.).

    Here's the regex I've come up with:

    private static final String C = "[\\Q><!\\E]?\\=?"; //comparator regex

    It looks for a ">", "<", or "!" once or not at all, followed by a "=" once or not at all. The "\\Q" starts a quote (because "<", ">", and "!" are reserved characters for regular expressions, I need to escape to use them) that is ended by the "\\E". The "\\" in front of the "=" is again an escape, because "=" is also a reserved regex character. All of these have two "\" because the compiler throws an illegal escape character otherwise; the first "\" lets it know that the second "\" should be taken literally.

    So, I whipped that up, thought it would work, and was sorely disappointed...

    I tested it using this simple code:
    public static void main(String[] teehee) {
      String s = "> < >= <= = !=";
      Matcher m;
      for(String part : s.split(" ")) {
        m = Pattern.compile(C).matcher(part); //C being the regex I described above
        System.out.print("\'" + part + "\': ");
        if(m.find()) {
          System.out.println("Valid!");
        } else {
          System.out.println("Invalid...");
        }
      }
    }

    Fortunately, when I ran that test, this was the result:
    '>': Valid!
    '<': Valid!
    '>=': Valid!
    '<=': Valid!
    '=': Valid!
    '!=': Valid!

    Unforunately, this regex gets so carried away that it accepts almost anything...
    Changing the value of 's' to ">as fqq<f >fq= <f=Q =D !s=", this results:

    '>as': Valid!
    'fqq<f': Valid!
    '>fq=': Valid!
    '<f=Q': Valid!
    '=D': Valid!
    '!s=': Valid!

    ...what have I done wrong? I'd appreciate any help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Regular Expression Difficulties...

    Try adding some boundary matchers in there...word boundaries, or as the below shows, beginning and ending of the line

    ^[\\Q><!\\E]?\\=?$

    Note the regex will also match an empty String (none of either)

  3. The Following User Says Thank You to copeg For This Useful Post:

    snowguy13 (July 3rd, 2012)

  4. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Regular Expression Difficulties...

    Oh, alright! That makes perfect sense! All of those other wrong inputs did match, because they contained the characters I asked for. Never did I realize my assumption that the Matcher would only start at the beginning... Thanks very much!!

    If I'm planning on putting this regex within other regex's (odd plural...), should I remove the "^" and "$"?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Regular Expression Difficulties...

    Here is a link the java tutorial on regular expressions: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    On another note, I read regex's as regular expressions to avoid the problem entirely, although oddly enough I read regex as it is spelt.

  6. The Following User Says Thank You to Tjstretch For This Useful Post:

    snowguy13 (July 4th, 2012)

  7. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Regular Expression Difficulties...

    Thanks for the link; but I've already been reading that extensively.

    I do the same with the pronunciation. Regex is so much easier to say than regular expression, though I sound awfully lazy saying that...
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. advanced regular expression question
    By chopficaro in forum Java Theory & Questions
    Replies: 5
    Last Post: June 8th, 2012, 03:03 PM
  2. Regular expression handling
    By marquiseoflight in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2012, 04:57 PM
  3. Regular Expression To Catch Duplicates
    By daniel_el in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 12:06 PM
  4. How to remove the regular expression from a string ?
    By srimanta in forum Java Theory & Questions
    Replies: 5
    Last Post: July 20th, 2011, 03:07 PM
  5. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM