简体   繁体   English

Java Swing JTable RowFilter忽略符号

[英]Java swing JTable RowFilter ignoring symbols

Here is my code for filtering the JTable . 这是我用于过滤JTable代码。 'text' is the input text to search in the table. 'text'是要在表中搜索的输入文本。

getSorter().setRowFilter(RowFilter.regexFilter("(?i)" + Pattern.quote(text)));

Here the filtering is case insensitive. 这里的过滤不区分大小写。 how can i ignore certain symbols like - ( ) + during the filtering like +88-11 & 8811 should match during filtering. 我如何在过滤期间忽略某些符号,例如- ( ) + ,例如+ 88-11和8811在过滤期间应该匹配。

Create a custom implementation of a RowFilter . 创建RowFilter的自定义实现。 In the include method: 在include方法中:

  • Call the replaceAll() method on your string to remove the special characters that you want to exclude. 在字符串上调用replaceAll()方法以删除要排除的特殊字符。
  • Then search for a match to your search pattern. 然后搜索与您的搜索模式匹配的内容。
  • Return a true/false depending on if it matches. 根据是否匹配返回true / false。

I am assuming that you will be able to adopt the following pattern(which completely satisfies your criteria) to your required pattern: 我假设您将能够采用以下模式(完全满足您的标准)来满足您的要求:

String pattern = "([\\Q+-*/\\E]?+\\d+)+"

Simple input: 简单输入:

+25+36
25+36*36
2536

And similar other, which will results in exactly one match. 与其他类似,这将导致一场比赛。

The way it works: 工作方式:

  1. [\\\\Q+-*/\\\\E] is to match any one of +, -, *, / and ?+ is put afterwards as a quantifier: a X?+ means that X occurs once or not at all. [\\\\Q+-*/\\\\E]要匹配+, -, *, /中的任何一个+, -, *, /然后将?+作为量词: X?+表示X一次出现或根本不出现。
  2. \\\\d+ is a pattern which matches one or more digits \\\\d+是与一位或多位数字匹配的模式
  3. and letting X = [\\\\Q+-*/\\\\E]?+\\\\d+ , (X) : matches X as a capture group and (X)+ means that the group will appear at least once or more. 并让X = [\\\\Q+-*/\\\\E]?+\\\\d+(X) :将X匹配为捕获组,而(X)+表示该组至少出现一次或多次。

Check out the Pattern class documentation . 查看Pattern类文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM