Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default [SOLVED]Regular expressions checking, by the format "*,txt",ab* etc'

    Hello
    I want to implement this function:
    boolean check(String str,String exp)
    that gets a string, and an expression and see if it mathches.
    But (!), according to a format by '*' (maybe similar to the SQL LIKE statement, but with * instead of %).
    Examples of tests of this function that should work:
    Java Code:
    boolean b3=check("55-210.txt", "55*");     // starts with 55
    assertEquals(b3,true);
    boolean b5=check("f1.txt", "*.txt");          // ends with *.txt
    assertEquals(b5,true);
    b=check("897", "8*8*");                         // 8...8...
    assertEquals(b,false);
    I don't mind, and even prefer to use existing external jars that can help, like Jakarta RegExp, JOSQL, and such.

    Thanks in advance
    Last edited by liran; 04-18-2010 at 10:17 PM. Reason: solved

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Sounds simple enough, I would suggest you take a look at this tutorial andd see what you come up with

    Introduction (The Java™ Tutorials > Essential Classes > Regular Expressions)

  3. #3
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    I saw it, and searched about Pattern,Matcher and such, but the format there is completely different.
    How can I implement my function ?

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    I'm sorry I didn't fully understand your question the first time around >.<'

    I have personally never used any of the existing external jars for Java Regular Expressions, I've always just translated the regular expressions into the Java Pattern ones.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The String class has 'startsWith' and 'endsWith' methods. Why not just use those?

  6. #6
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    actually you are right when there is only one '*', Maybe I will keep it simple, and implement it only for one.

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

    Default

    Quote Originally Posted by liran View Post
    I saw it, and searched about Pattern,Matcher and such, but the format there is completely different.
    How can I implement my function ?
    Those formats may be different, they're not totally different; you can translate your format to regular expression format in the following steps:

    Java Code:
    1) . ---> \.
    2) * ---> .*
    3) ? ---> .?
    Use the translated form for a Pattern compiler and you're in business.

    kind regards,

    Jos

  8. #8
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks JosAH ! This has almost worked for me. I did (just for trying, not for efficiency at this time) :
    *notice at the second line - what exactly did u mean? string does not accept "/".

    checkExp(String newStr,String exp):
    str = exp
    str = str.replace("*",".*");
    //str = str.replace(".","\\.");
    str = str.replace("?",".?");

    Pattern pattern = Pattern.compile(str);
    Matcher matcher = pattern.matcher(newStr);
    if(matcher.find()){
    return true;
    }
    return false;

    but this kinf of checking is still failing:

    b=check("a88b", "88*");
    assertEquals(b,false);

    How can i make that this test will also work ? maybe matcher.find() is not the right function to be called? maybe some other way ?
    thanks

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

    Default

    Quote Originally Posted by liran View Post
    Thanks JosAH ! This has almost worked for me. I did (just for trying, not for efficiency at this time) :
    *notice at the second line - what exactly did u mean? string does not accept "/".

    checkExp(String newStr,String exp):
    str = exp
    str = str.replace("*",".*");
    //str = str.replace(".","\\.");
    str = str.replace("?",".?");

    Pattern pattern = Pattern.compile(str);
    Matcher matcher = pattern.matcher(newStr);
    if(matcher.find()){
    return true;
    }
    return false;

    but this kinf of checking is still failing:

    b=check("a88b", "88*");
    assertEquals(b,false);

    How can i make that this test will also work ? maybe matcher.find() is not the right function to be called? maybe some other way ?
    thanks
    You didn't change the pattern the way I wrote; I corrected it for you:

    Java Code:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class REPattern {
    
    	private static boolean check(String name,String exp) {
    		exp= exp.replace(".", "\\.");
    		exp= exp.replace("*", ".*");
    		exp= exp.replace("?", ".?");
    
    		Pattern pattern = Pattern.compile(exp);
    		Matcher matcher = pattern.matcher(name);
    		
    		 return matcher.find();
    	}
    
    	public static void main(String[] args) {
    
    		System.out.println(check("file.txt", "*.txt"));
    	}
    }
    kind regards,

    Jos

  10. #10
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    Now I wrote as you have written. Almost all checks are working, but this kind of checks does not(!) work: (be should be false - should start with "88", but starts with "a", but it's true)

    b=check("a88b", "88*");
    assertEquals(b,false);

    How can I fix this ?

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

    Default

    Quote Originally Posted by liran View Post
    Now I wrote as you have written. Almost all checks are working, but this kind of checks does not(!) work: (be should be false - should start with "88", but starts with "a", but it's true)

    b=check("a88b", "88*");
    assertEquals(b,false);

    How can I fix this ?
    Add a fourth translation rule:

    Java Code:
    exp= "^"+exp;
    kind regards,

    Jos

  12. #12
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    Great, it's working, thanks a lot !!!!!!

    (I still found one check that fails:
    check("file1.txt.jpg", "*.txt");
    assertEquals(b,false);

    If it's not too complicated for you and you won't be mind, that will be great, but if not, I'll try it myself or will leave it this way.. thanks again !!!

    )

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

    Default

    Quote Originally Posted by liran View Post
    Great, it's working, thanks a lot !!!!!!

    (I still found one check that fails:
    check("file1.txt.jpg", "*.txt");
    assertEquals(b,false);

    If it's not too complicated for you and you won't be mind, that will be great, but if not, I'll try it myself or will leave it this way.. thanks again !!!

    )
    First you have to define the language recognized by your notation. This particular case should accept anything with ".txt" in a name/string? Shouldn't it accept anything ending with ".txt" instead? Please define the rules first then I'll see what I can do.

    For a first hack you can add a fifth rule:

    Java Code:
    exp= exp+"$";
    kind regards,

    Jos

  14. #14
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    First - you are right, now all my tests are working.

    As for the language: I don't know if it is well-defined, I tried to define it in my first message, you can look again. Basically, any char at "exp" will be as is in the string, and nothing else, except when there is '*' - when there is, anything can be there instead (I think it's like SQL "LIKE" operation).

    Maybe i'll give it a shot to define it:

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

    Default

    That's a very incomplete definition of 'globbing' indeed; globbing is a 'popular' shell form of regular expressions. Those 5 simple transformations I outlined turn a globbing expression to a Java regular expression so a Matcher object can do its job. Unix shell globbing is more powerful than what we've got now but if it meets your needs so be it.

    kind regards,

    Jos

  16. #16
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    I did not understand why my definition is "incomplete definition", but as we said, but it meets my needs so be it.

    Thank you ! :)

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

    Default

    Quote Originally Posted by liran View Post
    I did not understand why my definition is "incomplete definition", but as we said, but it meets my needs so be it.

    Thank you ! :)
    You're welcome of course; but don't you want a ? to match any single character then or any other of the globbing constructs? I assumed you do but assumptions are amost never correct ;-)

    kind regards,

    Jos

  18. #18
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    Yes I do :) It mathces the language I have defined. How can i do this? what shoud I add?
    This can be a nice addition (SQL "LIKE" also have this option)

    this is my function now:

    str = str.replace(".", "\\.");
    str = str.replace("*",".*");
    str = str.replace("?",".?");
    str = "^"+str;
    str = str+"$";
    Last edited by liran; 04-18-2010 at 08:36 PM.

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

    Default

    Quote Originally Posted by liran View Post
    Yes I do :) It mathces the language I have defined. How can i do this? what shoud I add?
    This can be a nice addition (SQL "LIKE" also have this option)

    this is my function now:

    str = str.replace(".", "\\.");
    str = str.replace("*",".*");
    str = str.replace("?",".?");
    str = "^"+str;
    str = str+"$";
    What is the definition of your language?

    kind regards,

    Jos

    ps you can replace the last two lines with str= "^"+str+"$"; it saves a few keystrokes ;-)

  20. #20
    liran is offline Member
    Join Date
    Apr 2010
    Posts
    18
    Rep Power
    0

    Default

    A new definition:


    Actually all the tests are working except the last one:

    b=check("abgcd", "ab?cd");
    assertEquals(b,true);

    b=check("abddcd", "ab?cd");
    assertEquals(b,false);

    check("alonggood.txt", "alon?good.*");
    assertEquals(b2,true);

    check("389-211-02.txt", "?89*");
    assertEquals(b3,true);

    //fails
    check("89-211-02.txt", "?89*");
    assertEquals(b3,false);

    I think i just need to fix the last one (or maybe some more...)
    Last edited by liran; 04-18-2010 at 09:14 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  3. have "regular" functions - with no class
    By itaipee in forum New To Java
    Replies: 2
    Last Post: 01-29-2009, 03:12 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Struts 1.x <bean:write format="0.00%"> help?
    By prabhurangan in forum Web Frameworks
    Replies: 0
    Last Post: 07-02-2008, 12:59 PM

Tags for this Thread

Posting Permissions

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