473,656 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with regular expression and parsing a sql-statement

hallo ng,

i am trying to parse a sql-statement with regular expressions.
my goal is to get the parts of the statement. in my thoughts the
group-functionality of the regular expressions should do this.

my pattern looks like:

A) string strPattern = "(select.+)(fro m.+)(where.+)?( order.+)?";

my searchstrings are like this:

1) string strText = "select * from x";
2) string strText = "select * from x where x.a=1";
3) string strText = "select * from x where x.a=1 order by x.a";
4) string strText = "select * from x order by x.a";

the resulting groups are:

3)
group 0: the statement
group 1: select *
group 2: from x where x.a=1 order by x.a

i don't understand the behaviour for the optional grouping. i am
expecting, that the optional patterns, e.g. (where.+)?, would be
grouped, if they are, and disappear, if they are not.

where is my fault?

whithout the ? in (where.+)? and (order.+)? im getting the wanted
result for 3).

thanks for helping, robert
Nov 15 '05 #1
2 2641


robert kurz wrote:

i am trying to parse a sql-statement with regular expressions.
my goal is to get the parts of the statement. in my thoughts the
group-functionality of the regular expressions should do this.

my pattern looks like:

A) string strPattern = "(select.+)(fro m.+)(where.+)?( order.+)?";

my searchstrings are like this:

1) string strText = "select * from x";
2) string strText = "select * from x where x.a=1";
3) string strText = "select * from x where x.a=1 order by x.a";
4) string strText = "select * from x order by x.a";

the resulting groups are:

3)
group 0: the statement
group 1: select *
group 2: from x where x.a=1 order by x.a

i don't understand the behaviour for the optional grouping. i am
expecting, that the optional patterns, e.g. (where.+)?, would be
grouped, if they are, and disappear, if they are not.

where is my fault?


Matching by default is greedy, meaning as much as possible is matched,
if you use
(from.+?)
then non-greedy matching meaning after "from" at least one abritrary
(".") character is matched but not all that are possible.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 15 '05 #2
Martin Honnen <Ma***********@ t-online.de> wrote in message news:<u3******* *******@TK2MSFT NGP10.phx.gbl>. ..
robert kurz wrote:

i am trying to parse a sql-statement with regular expressions.
my goal is to get the parts of the statement. in my thoughts the
group-functionality of the regular expressions should do this.

my pattern looks like:

A) string strPattern = "(select.+)(fro m.+)(where.+)?( order.+)?";

my searchstrings are like this:

1) string strText = "select * from x";
2) string strText = "select * from x where x.a=1";
3) string strText = "select * from x where x.a=1 order by x.a";
4) string strText = "select * from x order by x.a";

the resulting groups are:

3)
group 0: the statement
group 1: select *
group 2: from x where x.a=1 order by x.a

i don't understand the behaviour for the optional grouping. i am
expecting, that the optional patterns, e.g. (where.+)?, would be
grouped, if they are, and disappear, if they are not.

where is my fault?


Matching by default is greedy, meaning as much as possible is matched,
if you use
(from.+?)
then non-greedy matching meaning after "from" at least one abritrary
(".") character is matched but not all that are possible.


hallo martin,

thank you for your answer.

in my opinion the problem is devided in two parts. the first is, that
non-optional groups are one level higher than optional. the second is,
that .+ is greedy, so the optional group is not taken.

do i think the right way or am i wrong? if i'm right, i don't have an
idea to solve my sql-problem.

robert
Nov 15 '05 #3

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

Similar topics

7
2588
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form field beside name. I want to get the following but not sure how to modify the code below. 1. Field Name (to appear beside NAME:) 2. Field Type (to appear beside TYPE:)
4
3219
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
4
2172
by: Ben Dewey | last post by:
Hey, I have only been playing with regular expressions for some time. I am working on some code that parses and object 560 event log. I have created two expressions the first one which works okay is for the actual csv of each log. The second one parses out the description of the log. My problem is with the accesses section of the description. How do I parse multiple groups that have the same name. When I do a for each through the...
4
395
by: rufus | last post by:
I need to parse some HTML and add links to some keywords (up to 1000) defined in a DB table. What I need to do is search for these keywords and if they are not already a link, and they are not inside a paragraph tag, ie <p class=tab>, I convert it to a link. There are also 8 other conditions to decide whether text is converted to a link. Is there an easy way to compare match collections that are returned from separate regular...
2
3012
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text index on one column, and punctuation in the column was causing some problems down the line. This column is used only for full text indexing, and otherwise ignored. I decided to use the following regular expression to remove all punctuation (actually...
6
2287
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not work: it seems that < is not a valid character, so the beginning of the word starts at theletter 'p' instead of '<'. Because I'm not an expert in regular expressions, maybe someone of you guys can help me? I need the correct regex to find the...
6
1406
by: John Salerno | last post by:
Ok, this might look familiar. I'd like to use regular expressions to change this line: self.source += '<p>' + paragraph + '</p>\n\n' to read: self.source += '<p>%s</p>\n\n' % paragraph Now, matching the middle part and replacing it with '%s' is easy, but
25
5147
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
1601
by: Buglish | last post by:
Hi, Task : -Capture a HTML table with use of regular expression from a text string buffer(entire document). –Pass it to another function to create a multi dimension array out of it. - Pass it to another function to build it into a RTF table. -Then return it back to the regular expression to replace the search result. Difficulty: -The regular expression doesn’t pass the back referenced table fields to the next function.
9
2482
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div class=\"notice_tan\">(.| \s)*</div>/", "", $site); I'm trying to remove from the very top to a specific div Top of file:
0
8816
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
8717
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
8498
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
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1600
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.