...
This noncompliant code example defines a method splitWords()
that finds matches between the String
literal and the input sequence. Because '\b' is the escape sequence for a word boundary, the misleading notion that String
literals can be used as is, can convince the implementer that the pattern matches to the word boundaries and as a result, splits a given string into individual words. Instead, the string WORDS
silently compiles to a backspace character.
Code Block | ||
---|---|---|
| ||
import java.util.regex.Pattern;
public class BadSplitter {
private final String WORDS = "\b"; // Intend to split on word boundaries
public String[] splitWords(String input){
Pattern p = Pattern.compile(WORDS);
String[] input_array = p.split(input);
return input_array;
}
}
|
...
This compliant solution shows the correctly escaped value of the String
literal WORDS
that results in a regular expression designed to split on word boundaries.
Code Block | ||
---|---|---|
| ||
import java.util.regex.Pattern;
public class GoodSplitter {
private final String WORDS = "\\b"; // Will allow splitting on word boundaries
public String[] split(String input){
Pattern p = Pattern.compile(WORDS);
String[] input_array = p.split(input);
return input_array;
}
}
|
...