Question

RowFilter.regexFilter seems like a an easy way to filter my JTable.

I'd like to let people type in filters in a GUI that are straight-up text (i.e. not regex expressions) and then filter based on them. Like this:

RowFilter<TableModel, Object> filter = RowFilter.regexFilter(".*" + pUserFilterString + ".*", pColumn);

This would be great if the user carefully quoted his filter text to make it a valid regular expression, but they won't be thinking about regular expressions -- just text. If there were an easy way to do it, I would like to escape anything in their String that wouldn't be treated literally by the regex parser (e.g. "*" -> "\*").

Is this possible / worth it or should I be going another direction with my code?

Was it helpful?

Solution

This operation is called quoting. You can get the escaped string by calling...

String escapedString = Pattern.quote(inputString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top