• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex to filter any word in any order in column of JTable?  RSS feed

 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created filters for every column in my Jtable however, some of these columns have more than one word inside of them and my filters will only filter them based on the first word. For example if I had a first and last name in one column, it will filter the table if I enter the first name in my filter text field but it will not filter that same column if I only input the last name. What is a good regex expression to filter any word in any order?

 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
additional information
idfilter, fromFilter, toFilter and etc. are text fields and each of them have a document listener that calls this filter row method
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first you need to split apart the words so you can search for them separately. Try String.split(<regex>) for that. Then loop through the array it returns and see if it matches that word.

Another less precise way would be to use Pattern.matcher.find() on the whole string. Be careful about reset on the macther.
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok ill try that but does splitting the string itself mean I have to create more rowfilters?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't used row filter before but looking at it, I don't understand the regexes you're passing it.

RowFilter.regexFilter("bees")

Should return a row if the string is "honey bees" or "bees of terror" or any string the has "bees" in it. So tell me what is

RowFilter.regexFilter("^" + "(?i)" + "^" + filterId, 0);

? Because this doesn't look like a legal regex to me. Can you explain it in words?
 
Abh Nai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out all I had to do was remove ^s and it worked.

^ represents beginning of the line,
?i represents that the regex is not case sensitive

RowFilter.regexFilter( + "(?i)" + filterId, 0);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!