Open kammaii opened 4 years ago
@Test
public void simpleRegex() {
List<String> words = new ArrayList<String>();
words.add("Student");
words.add("Teachers");
words.add("Study");
words.add("school");
for(String word : words) {
Pattern pattern = Pattern.compile("^st", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(word);
while (matcher.find()) {
String matchedLetters = matcher.group();
System.out.println("Word: " + word + ", Matched Letters: " + matchedLetters);
}
}
}
Hi Danny, this actually might be better. Use if
instead of while
. If you use while
, you might get multiple results for the same vocabulary word.
@Test
public void simpleRegex() {
List<String> words = new ArrayList<String>();
words.add("Student");
words.add("Teachers");
words.add("Study");
words.add("school");
for(String word : words) {
Pattern pattern = Pattern.compile("^st", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(word);
**if(matcher.find()) {**
String matchedLetters = matcher.group();
System.out.println("Word: " + word + ", Matched Letters: " + matchedLetters);
}
}
}
I made searching flashcards feature. For example, if I want to find the word 'Student', I want to make it could be found without care of upper or lower case. I code below and it works whatever I put 'student', 'STUDENT', 'Student' in. (I couldn't make 'STdent' though.) But my code seems not clean because I used a lot of '||'. I know the method 'equalsEgnoreCase' but it's not real time. Because it should find every words contain 's' as soon as I put 's' in. My question is...