Reputation: 572
I am curently trying to write a method, which checks within a textfile if a specific word is encapsulated by specific charachter-sets. For instance, if my keyword is "blablabla", and the char set are html title-tags, (e.g.
<h2> blabla </h2>
), the method is supposed to return true. However, the keyword itself can be surrounded by different keywords (e.g.
<h2> something something blabla in the month of may </h2>
) in which case the method still has to return true, since the keyword is still surrounded by the charset defined. Here is what I allready have:
public static Boolean KeywordIsInTitle(String text, String keywordInText){
boolean isInTitle = false;
if( text.contains(keywordInText) == true){
/*Here is wehre I am stuck....
* */
isInTitle = true;}
return isInTitle;
}
I have been looking at regex and pattern matching for an hour or so but nothing works, and I have to admit I don't ffel very comfortable and very familiar with theses concepts yet... Can anyone help? thank you very much in advance!
Upvotes: 0
Views: 417
Reputation: 1162
import java.util.regex.Pattern;
public class Match {
public static void main(String[] args) {
String s1 = "<h2> blabla </h2>";
String s2 = " <h2> some other string </h2>";
final String regex = "<h2>(.)*blabla(.)*<\\/h2>";
boolean b1 = Pattern.matches(regex, s1);
boolean b2 = Pattern.matches(regex, s2);
System.out.printf("the value of b1 is %b\n", b1);
System.out.printf("the value of b2 is %b\n", b2);
}
}
Upvotes: 1