Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove Non Alpha Nums from JTable Filter

 
Neil Barton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I currently use this statement to filter entries in a jtable:
rf = RowFilter.regexFilter("(?i)" + Searchtext);
This works and searches for the Searchtext disregarding case. However, I need it to ignore spaces and non-alphanumerics as well. This would be so that if someone searches for "2you" it would retrieve
2 you transport
(note the spaces) and
2-u-quick


I thought (w?i) would work as I thought it would pull in only alphanums and ignore spaces but it doesn't. Is there a regular expression that can be used in a rowfilter to ignore spaces etc? If not, is there another way to do it?
 
L Foster
Ranch Hand
Posts: 185
10
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Neil, that looks like a clever idea. Restated, it sounds like you wish to allow users to text-to-filter, but then turn your filter into a space-exploded version of the original, and retain entries that contain any combination of words which might match that. It's actually asking a bit more than superficially appears.

For example, "feetoil" would match "fee toil", or "feet oil", or even "fee to il". And you are also allowing the word-search to be delineated beginning or end. "feetoil" could then match "fee to illness consultant".

What might be helpful in this case, is pre-excising the targets of your searches. The contents of the rows you are filtering first get all their spaces removed (and you could even make them all lower case), and then you could apply the filter (which might also be pre-excised). Looking at the RowFilter example in its Java Doc (http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html ), I see that you do get to apply some complexity to your code there (you don't have to just pass in a string-regex). So I think that could work out for you.
 
Neil Barton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that, it pointed me in the right direction. in the end I did this, which seems to work...

Thanks again.
 
Paweł Baczyński
Bartender
Posts: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write: Write:
Don't write:or:Write:
Your code:can be rewritten as:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic