Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search using Regex in Entity Framework

I want to search for a string pattern in a database table.

The search pattern is simply the string entered by the user. However the field to be searched can have spaces in between, or user might enter spaces in the string.

So I want to create a regex, such that if say the string is "Test", I look for regex which might have spaces in between, something like this

T[\s]*e[\s]*s[\s]*t.

Can anyone please suggest how I can do this in Entity framework - or by using ESQL?

like image 327
gunnerz Avatar asked Sep 04 '26 22:09

gunnerz


1 Answers

For that pattern, you don't need a Regex. You're best off trimming them out, then doing the search. You could so something like:

SELECT * FROM MyTable
WHERE REPLACE(MyColumn, ' ', '') LIKE '%' + REPLACE(@YourVariable, ' ', '') + '%'
like image 115
mattytommo Avatar answered Sep 07 '26 10:09

mattytommo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!