Reputation: 12743
I have to lookup for a word "age" and similar word in a text file.
I have following sentence :
String.contains always return true in each case. My requirement is to pass the first five sentence and it return false in last case.
I will solve this problem by writing some code which contains a bunch of string " age ", " age." , "ages", "aged", " age," etc..
Is there any better way to solve this problem.
Upvotes: 3
Views: 1701
Reputation: 133
What you need is called a regular expression (or regex)
Here's a perfectly detailed definition of regular expressions and use in Java, which can be done with matches(String Regex) method of String class.
For your example, it could (normally) be : myString.matches(".*age? .*")
.
Pay attention in escaping special characters in Java. You can try your regexs here. I didn't do it in the example above, but you can try :)
In detail :
Hope it helped.
Upvotes: 1
Reputation: 7462
A naive solution (expensive) would be the following:
The edit distance of two string is the number of edits (additions, deletions and replacements) that are required to make one string equal to the other. You can find an implementation of edit distance in the simmetrics library, or maybe elsewhere, too.
Another option could be to stem the words at step 2 and use contains with the stemming of the word age (also expensive).
If you already know all the acceptable answers (or at least their pattern), go for Avinash Raj's answer.
Upvotes: 1
Reputation: 174696
If you use regex, you have to put all the possiblities.
string.matches("(?i).*\\bage[ds]?\\b.*");
Upvotes: 3