开发者

Easy way to escape regex character classes in an arbitrary String?

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 t开发者_JS百科ext. 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?


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

String escapedString = Pattern.quote(inputString);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜