473,503 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prb Match RegEx

Hi,

I would like to build a regex that returns me the table list of a SQL SELECT
request like :
-SELECT * FROM tab1, tab2, tab3
-SELECT * FROM tab1, tab2 WHERE col="abc"
-SELECT * FROM tab1, tab2 LIMIT 1

I tried something like that :
---
Regex pattern1 = new
Regex(@"FROM\s+(?<tabname>.*)(WHERE|LIMIT|ORDER|HA VING|GROUP)?",
RegexOptions.IgnoreCase );
Match match1 = pattern1.Match(request);
if( match1.Success )
string res = match1.Groups["tabname"].Value;
---

But, each time, in tabname, I get all the string to EOF.
For ex , in SELECT * FROM tab1, tab2 LIMIT 1
I get "tab1, tab2 LIMIT 1" instead of "tab1, tab2"
Thanks,
Horizon
Nov 16 '05 #1
2 1463
"Horizon" <mi****@gmail.com> wrote in
news:41***********************@news.free.fr...
Hi,

I would like to build a regex that returns me the table list of a SQL
SELECT request like :
-SELECT * FROM tab1, tab2, tab3
-SELECT * FROM tab1, tab2 WHERE col="abc"
-SELECT * FROM tab1, tab2 LIMIT 1

I tried something like that :
---
Regex pattern1 = new
Regex(@"FROM\s+(?<tabname>.*)(WHERE|LIMIT|ORDER|HA VING|GROUP)?",
RegexOptions.IgnoreCase );
Match match1 = pattern1.Match(request);
if( match1.Success )
string res = match1.Groups["tabname"].Value;
---

But, each time, in tabname, I get all the string to EOF.
For ex , in SELECT * FROM tab1, tab2 LIMIT 1
I get "tab1, tab2 LIMIT 1" instead of "tab1, tab2"


That's not surprising: "*" is a "greedy quantifier", i.e. it will match the
longest possible string. As "." matches any character, and everything after
that is optional, the leftmost longest match will be the whole string.

I'd suggest using a lazy quantifier (*?), and making the alternation
non-optional, like this:
Regex(
@"FROM\s+(?<tabname>.*?)(WHERE|LIMIT|ORDER|HAVING| GROUP|$)",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
);

Hope this helps,

Niki
Nov 16 '05 #2
Yes ! It is exactly what I needed.
Thanks for your help :)

Horizon
Nov 16 '05 #3

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

Similar topics

2
7898
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be...
3
5246
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
1
7393
by: Laser Lu | last post by:
Hi, all, I'm now writing a program to compress JavaScript code. One puzzle is how to write a regular expression to find out and remove all the redundent blank spaces. However, those blank spaces...
1
2034
by: tdmailbox | last post by:
I have the following regular expression. It works fine if the regex code returns a match. However if not the .match code fails. How can I code this so that it skips the match if the regular...
38
15911
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
5
10163
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is...
4
2621
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
8
10262
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
4
3133
by: Peter | last post by:
Hi all, I am searching through directories trying to find the prefix to a number of files. Unfortunately the files don't have a standard naming convention yet. So some of them appear as:...
5
8748
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of...
0
7323
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...
1
6984
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...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
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...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
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 ...
0
377
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...

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.