mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
740 stars 55 forks source link

[Question] Use regular expressions in the search box #53

Closed matrs closed 2 years ago

matrs commented 2 years ago

Hello, thanks for this library, it's been really useful.

I'm looking to use regular expressions as input for the search box. There is an example here, where you mark the regex box and the search box works as expected. Is it possible to add that function to my code? I really don't know how to go about it and any help/example is appreciated (I dont'care for the option boxes, if the default search box understand regular expressions would be more than enough).

Also, in the documentation about the [API search()](https://datatables.net/reference/api/search()), says:

function search( input [, regex[ , smart[ , caseInsen ]]] )
Description:
Set the global search to use on the table. Note this doesn't actually perform the search, but rather queues it up - use draw() to perform the search and display the result.

Where one of the parameters is:

regex | boolean | Yes - default:false
Treat as a regular expression (true) or not (default, false).
mwouts commented 2 years ago

Hi @matrs , well I am afraid it is going to be difficult to port either

as they both involve additional HTML fields (i.e. the search filter is not taken from the search box of the table), and also require additional javascript that at the moment we can't really fit that into our table template.

I think what you are looking for is more something like the search.regex option. You can pass it to your datatable like this:

show(df, search={"regex": True})

or

import itables.options as opt
opt.search={"regex": True}

This option does has some impact, cf. the search for * which returns the original table on the second cell (and no result when search.regex=False): image

However I must say that I have not been able to search for specific end of words so maybe the exact way to use it is a bit more complex. Maybe you'll have to deactivate the search.smart parameter, e.g.

show(df, search={"regex": True, "smart": False})

All the possible search options are listed here. Please keep us posted and let us know what works for you! I'll be happy to update the README when we have an example that works well.

matrs commented 2 years ago

Hi @mwouts , thank you for your prompt answer. The option search={"regex": True} is what I was looking for and until now it has worked as expected (I was specially interested in | ).

About the "end of word" pattern, without disabling the smart search, it works: wound\b returns only wound, but wound returns wounding too. Maybe you are referring to something else?

mwouts commented 2 years ago

Oh great! I'll add the example to the README, then.

About the "end of word" pattern, without disabling the smart search, it works: wound\b returns only wound, but wound returns wounding too. Maybe you are referring to something else?

Yes, I wanted to use the ^ or $ boundaries, but my attempts were unsuccessful (all I got was an empty table when I was expecting a match). Let me know if they work for you!

matrs commented 2 years ago

I see. With $ and ^ I'm getting the same result as you, empty tables.