Solved

C function that behaves exactly like the SQL LIKE function

Posted on 2004-09-10
7
193 Views
Last Modified: 2010-04-15
Hi,

Does anyone know of a function or can provide an code sample of a C function that behaves exactly like the SQL LIKE function?  The function should accept and properly evaluate all wildcards and special operators used by the SQL LIKE ( %, ^, [], etc ).

Thanks In Advance.
0
Comment
Question by:CUGroup
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12033134
Hi CUGroup,

If you can describe the exact behaviour or SQL LIKE and tell us the platform that you are working on, hopefully we can come up with something
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12043087
Hi,

It should be relatively straight forward to convert from SQL syntax to standard Regular Expression syntax (man regex on Unix)

e.g.

_ -> .
% -> .*

etc.
the write wrapper function(s) on the regex library calls, to convert the LIKE string to the regular exp. and to process from that...

Hope this helps,
C.
0
 

Author Comment

by:CUGroup
ID: 12069571
Hi,

Thanks for the responses.  Unfortunately, I need function to be portable, UNIX and Windows so I'm in search of a c function to mimic this sql function.

This is the best resource I could find regarding the use of the SQL LIKE function.

http://www.sql-tutorial.net/SQL-LIKE.asp

Thank you for your help.

0
 
LVL 2

Accepted Solution

by:
sneeuw_chan earned 125 total points
ID: 12070444
I think it corresponds more to 'glob' than to 'regex'.
(Glob is the way that a dos-prompt or unix shell matches filenames on the commandline.)

You could try looking for a 'glob'  library.
A quick google shows this, for example:  http://www.cs.yorku.ca/~oz/glob.bun
(You could even change that code to parse this 'LIKE' format directly, just change the case '*'  to case '%' and the case '?' to case '_'.)

If that's not to your liking, search for other glob libraries.
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 125 total points
ID: 12072899
CUGroup, if you are just looking for a portable (Unix and Windows) way of matching patterns in text, then surely the standard Posix regex functionality I listed would be the way to go? I'd be confident that there is a Windows inplementation (defintely if you use a GCC derived compiler)
Either that, or as sneeuw_chan suggested use Glob functionality.
I wouldn't go with the SQL LIKE format, unless you are actually implementing a SQL engine... LIKE is a lot more cumbersome than regex or glob...
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now