Search String for exact match (a GREP program)
Hi all, i'm having some problems with creating a Grep clone.
The only problem is, checking if a string has the exact match inside. I've already tried indexOf, but it returns a substring (for an example if we search the word program and the string has a word programming, it will not be right).
I've googled out the Matcher methods, but they seem to do the same. Or maby am i'm using them incorrectly.
In short: what is the best way to search a line for an exact match of the word/string
Thank you for your advice.
[548 byte] By [
Stiflera] at [2007-11-27 5:13:25]

if (line.indexOf("wordSearched") != -1)System.out.println(line+" contains the word wordSearched!");
nope calvino, i am fammiliar with this method, but:
if i load a line "welcome to java programming" (on which i will perform my search) and input a word "program" (a word which i will search for), the indexOf will find "program", beacouse it is a substring of the word "programming".
that is not what i want :)
I want to find the exact match in the line. if i return to the previous example, "program" would be found if the line were "welcome to java program".
if you understand what i mean, but thanks anyway.
any other suggestions please?
http://java.sun.com/docs/books/tutorial/essential/regex/
what about :
if (
(line.indexOf(" program ") != -1) ||
(line.indexOf(" program\r") != -1) ||
(line.indexOf(" program\n") != -1) ||
(line.indexOf("\nprogram ") != -1)
)
? ^^
calvino:
I thought about this also, but this way is not bulletproof, there are always some exceptions that will make sure that the program does not work :)
cotton:
thank you for the link, i think i found out a easy solution. i just hope the teacher will like it, and wont be able to find any bugs :D
anyway, if anyone will search for this, i did this:
i limited the word, using metacharacters (not sure if this is really the proper name :P) to limit the word which the program searches for.
searchWord="\\b"+searchWord;
searchWord=searchWord+"\\b";
(searchWord turns out to "\\bprogram\\b" and is limited by the "\\b" on each side)
after that, searchWord is used in
Pattern.compile(searchWord);
Matcher pm=pattern.matcher(lineToBeSearched);
and found by
pm.find();
that is it, i will have to test it even more, but it looks good enough, although it's not fancy :)
anyway, if i remembered the mail to which i registered my previous account, i'd gladly give each of you Duke Stars, but this way, i can only thank you.
Thank you! :) Many internets for both of you :D
Message was edited by:
stifler11
> cotton:
> thank you for the link, i think i found out a easy
> solution. i just hope the teacher will like it, and
> wont be able to find any bugs :D
>
Well you can't get more GREP than using RE.
> anyway, if i remembered the mail to which i
> registered my previous account, i'd gladly give each
> of you Duke Stars, but this way, i can only thank
> you.
cotton gets shafted again! :(
> Thank you! :) Many internets for both of you :D
>
You're welcome, glad to help.