Results 1 to 20 of 24
- 04-15-2010, 01:29 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
[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:
I don't mind, and even prefer to use existing external jars that can help, like Jakarta RegExp, JOSQL, and such.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);
Thanks in advanceLast edited by liran; 04-18-2010 at 10:17 PM. Reason: solved
- 04-15-2010, 03:21 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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)
- 04-15-2010, 03:26 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
I saw it, and searched about Pattern,Matcher and such, but the format there is completely different.
How can I implement my function ?
- 04-15-2010, 03:48 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 04-16-2010, 05:48 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The String class has 'startsWith' and 'endsWith' methods. Why not just use those?
- 04-16-2010, 09:27 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
actually you are right when there is only one '*', Maybe I will keep it simple, and implement it only for one.
- 04-16-2010, 11:12 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Those formats may be different, they're not totally different; you can translate your format to regular expression format in the following steps:
Use the translated form for a Pattern compiler and you're in business.Java Code:1) . ---> \. 2) * ---> .* 3) ? ---> .?
kind regards,
Jos
- 04-17-2010, 11:22 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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
- 04-18-2010, 08:34 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You didn't change the pattern the way I wrote; I corrected it for you:
kind regards,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")); } }
Jos
- 04-18-2010, 04:28 PM #10
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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 ?
- 04-18-2010, 04:35 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-18-2010, 06:02 PM #12
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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 !!!
)
- 04-18-2010, 06:18 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:exp= exp+"$";
Jos
- 04-18-2010, 07:44 PM #14
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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:
- 04-18-2010, 07:58 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-18-2010, 08:09 PM #16
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
I did not understand why my definition is "incomplete definition", but as we said, but it meets my needs so be it.
Thank you ! :)
- 04-18-2010, 08:19 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-18-2010, 08:31 PM #18
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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.
- 04-18-2010, 08:41 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 04-18-2010, 09:06 PM #20
Member
- Join Date
- Apr 2010
- Posts
- 18
- Rep Power
- 0
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.
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
have "regular" functions - with no class
By itaipee in forum New To JavaReplies: 2Last Post: 01-29-2009, 03:12 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Struts 1.x <bean:write format="0.00%"> help?
By prabhurangan in forum Web FrameworksReplies: 0Last Post: 07-02-2008, 12:59 PM


LinkBack URL
About LinkBacks


Bookmarks