472,784 Members | 957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

Using Regex to create a SQL's "like" like function.


Hi,

I need to write a function that should behave like the SQL's "like" operator
on a list of words.
I was wondering if I can use Regex directly to do this job. But I've been
reading about regex and it supports very different characters and behaves
different.

So, I'm looking for an advise. I'd like to know if it is feasable or not, I
mean, if dotnet Regex implementation could handle it.
Or, if anyone can quote a link for a sample or a code snippet to start with,
it'd be great !

Thanks in advance,
CK

Nov 16 '05 #1
1 19556
"Craig Kenisston" <Cr************@hotmail.com> wrote in
news:e3**************@TK2MSFTNGP10.phx.gbl:

Hi,

I need to write a function that should behave like the SQL's
"like" operator on a list of words.
I was wondering if I can use Regex directly to do this job. But
I've been reading about regex and it supports very different
characters and behaves different.

So, I'm looking for an advise. I'd like to know if it is
feasable or not, I mean, if dotnet Regex implementation could
handle it. Or, if anyone can quote a link for a sample or a code
snippet to start with, it'd be great !


Craig,

See if this works:

/* Ex:
*
* bool isMatch =
* IsSqlLikeMatch("abcdef", "[az]_%[^qz]ef");
*
* should return true.
*/

private bool IsSqlLikeMatch(string input, string pattern)
{
/* Turn "off" all regular expression related syntax in
* the pattern string. */
pattern = Regex.Escape(pattern);

/* Replace the SQL LIKE wildcard metacharacters with the
* equivalent regular expression metacharacters. */
pattern = pattern.Replace("%", ".*?").Replace("_", ".");

/* The previous call to Regex.Escape actually turned off
* too many metacharacters, i.e. those which are recognized by
* both the regular expression engine and the SQL LIKE
* statement ([...] and [^...]). Those metacharacters have
* to be manually unescaped here. */
pattern = pattern.Replace(@"\[", "[").Replace(@"\]",
"]").Replace(@"\^", "^");

return Regex.IsMatch(input, pattern);
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: NK | last post by:
Hi all, Does anyone know of how I can disable case sensitivity for the LIKE function in SQL? Currently the SQL statement looks like: $query = "SELECT * FROM itrader_games WHERE...
2
by: Adam Short | last post by:
I've never needed it before but now I do! Does anyone know if you are able to perform a NOT Like search using Classic ASP ADO? i.e. myData.Filter = "Ref NOT LIKE '*1234*'" by the way this...
10
by: joshsackett | last post by:
I am starting an encryption project for my database and I'm performing some tests on decryption speed. A lot of my application queries use a LIKE parameter in the WHERE clause. To keep from...
2
by: Dave Smithz | last post by:
Hello there, Summary: How far can you go with SQL Select queries using like clauses with wildcard characters. Can you apply anything like regular expressions? Full details: On a Intranet...
2
by: Noah | last post by:
If I want to do an update query with the critera being that a field contains a certain string anywhere within, how would I write that? Would that be a case of using "like"? For example I want...
2
by: craigkenisston | last post by:
Hi, I'm looking to achieve the same functionality as the "like" function in SQL (Server) in a program in C#. Is there anything out of the box ready to use ? Can this be achieved with regex...
5
by: brino | last post by:
hi all ! i want to use the "like" function in a query but i want the user to enter the search variable. i have tried "like" but this doesnt return any results for some reason. any ideas ??? ...
4
by: kathnicole | last post by:
Hi All, I would like to retreive all the records that starts, where the field CompanyName in table tblJob start with "sap*"... i assume this can be acheived by using LIKE function in query design...
4
by: teddysnips | last post by:
I am just getting to grips with LINQ to SQL and my first attempt is to create a Search form. I need the LINQ to generate SQL like this: SELECT * FROM CustomerTable WHERE Surname LIKE 'S%' ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.