473,898 Members | 3,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19867
"Craig Kenisston" <Cr************ @hotmail.com> wrote in
news:e3******** ******@TK2MSFTN GP10.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(pa ttern);

/* 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(i nput, 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
48346
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 console='$console' AND genre='$genre' AND title LIKE '%$title%' ";
2
5612
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 doesn't work!! and neither does myData.Filter = "Ref <> LIKE '*1234*'"
10
7604
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 changing my application I am performing all the work on the back-end; creating views, triggers and UDFs to encrypt/decrypt the data. A problem has arisen around the LIKE parameter, though. Currently: SELECT SSN, FNAME, LNAME FROM USERS WHERE LNAME...
2
13368
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 website, I request a code from a user which is then compared with a code in an ADO access database.
2
1527
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 to have the criteria met if the string "Small" is anywhere in the field. So any of these would qualify: Short Small Medium Small Small
2
2666
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 ? If so, how ? Samples welcom. Thanks,
5
2407
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 ??? thanks brino
4
2581
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 but i want the user to enter the text. Say, there are two company Names like "sapin" and "sapeen" when the user enters the text as "sap", it should list all the records where the company name starts with Sap.. and again, if the user enters,...
4
17788
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%' My LINQ so far looks like this: var cons = from c in db.CustomerTable
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10858
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4296
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.