protected void searchFilter(String s, int n)
{
RowFilter<MyTableModel, Object> rf = null;
try {
System.out.println(s);
rf = RowFilter.regexFilter(s, n);
} catch (PatternSyntaxException e) {
System.out.println(e);
}
filters.add(rf);
}
I am trying to match a string in a JTable, that contains parentheses. In the above code, the string parameter could be:
John (Smith)
And the column i’m searching in:
Jane (Doe)
John (Smith)
John (Smith)
Jack (Smith)
Where I want it to return:
John (Smith)
John (Smith)
But right now it doesn’t return anything. I have looked at the documentation for Matcher, Pattern and RowFilter, but nothing has helped me so far.
Parentheses are meta-characters in regular expressions. Hence you are actually trying to match
John Smith(without parentheses). What you need to do is to escape them.Java has a built-in function to escape all meta-characters automatically:
Pattern.quote. Runsthrough this function and it should fix it.Also note that you might want to surround the pattern with
^...$. Otherwise it would accept rows containing something likeThis is John (Smith) foobar.(because the regex is glad if it can match a substring of the input).