473,499 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExpert required

Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.

Jul 24 '06 #1
6 1035
* pigeonrandle wrote, On 24-7-2006 21:43:
Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.
This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse
Jul 24 '06 #2
Jesse,
Thankyou for replying. Unfortunately, i cannot seem to get it to work
.... here is the code i'm using:

private void button1_Click(object sender, System.EventArgs e)
{
String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";
String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+*)'\s*\)";

Regex rex = new Regex(sRX);

foreach(Match m in rex.Matches(sSQL))
{
MessageBox.Show(m.Value);
}

}
Thanks again,
James.

Jesse Houwing wrote:
* pigeonrandle wrote, On 24-7-2006 21:43:
Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.

This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse
Jul 24 '06 #3
* pigeonrandle wrote, On 24-7-2006 23:01:
Jesse,
Thankyou for replying. Unfortunately, i cannot seem to get it to work
... here is the code i'm using:

private void button1_Click(object sender, System.EventArgs e)
{
String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";
String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+*)'\s*\)";

Regex rex = new Regex(sRX);

foreach(Match m in rex.Matches(sSQL))
{
MessageBox.Show(m.Value);
}

}
It will return one match with all the results in a group:

String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";

String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)";
Regex rex = new Regex(sRX, RegexOptions.IgnoreCase);

Match m = rex.Match(sSQL);
foreach (Capture c in m.Groups["value"].Captures)
{
Console.WriteLine(c.Value);
}
Console.ReadKey();

Thanks again,
James.

Jesse Houwing wrote:
>* pigeonrandle wrote, On 24-7-2006 21:43:
>>Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.
This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse
Jul 24 '06 #4
Jesse,
Thanks again. I don't know if it's my age, general stupidity or the
fact that i read your last post with my eyes shut |o). That would be
how to do what you suggested!

Thanks again,
James
Jesse Houwing wrote:
* pigeonrandle wrote, On 24-7-2006 23:01:
Jesse,
Thankyou for replying. Unfortunately, i cannot seem to get it to work
... here is the code i'm using:

private void button1_Click(object sender, System.EventArgs e)
{
String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";
String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+*)'\s*\)";

Regex rex = new Regex(sRX);

foreach(Match m in rex.Matches(sSQL))
{
MessageBox.Show(m.Value);
}

}

It will return one match with all the results in a group:

String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";

String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)";
Regex rex = new Regex(sRX, RegexOptions.IgnoreCase);

Match m = rex.Match(sSQL);
foreach (Capture c in m.Groups["value"].Captures)
{
Console.WriteLine(c.Value);
}
Console.ReadKey();

Thanks again,
James.

Jesse Houwing wrote:
* pigeonrandle wrote, On 24-7-2006 21:43:
Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.

This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse
Jul 25 '06 #5
Hi James,

I don't believe that Regular Expressions alone are your solution here. First
of all, you haven't thought about all of the possible combinations of values
that you might need to capture. For example, you example indicates that all
text columns will be used, and therefore, all values will be surrounded with
single quotes. But what if the column is a numeric or datetime type? There
will be no single quotes. In this case, both the values in the first set of
parentheses, the column names, will match as well as the values in the
VALUES clause. In fact, it is necessary to discriminate between values which
are contained within parentheses and values that are not. In addition, there
arequite a few possible combinations of characters that would have to be
accounted for only withing parentheses, and using capturing groups would
reduce the number of items in each match that were in any given group to 1.
Here are some of the possible combinations you would have to deal with:

('value'
( 'value'
( value
(value
,'value'
, 'value'
,value
, value
'value')
'value' )
value)
value )
,value)
, value)
, value )
,'value')
, 'value')
,'value' )
, 'value' )

And you would need to eliminate the extraneous punctuation, which would
necessitate using grouping or positive and negative lookarounds. Note that
half of these examples would match for the first set of parentheses, the
column names. And don't forget the syntax rules of SQL, which indicate that
a text value may contain single quotes, but that they must be escaped by
doubling, and it may also contain commas. A numeric or datetime value may
not contain either commas or single quotes (unless it is in european format,
in which case you would have to allow for commas).

Now, a kluge could be worked out by using a large number of patterns ORed
together with no grouping, but you're getting into territory there which
would cause the use of a single regular expression to be much less efficient
than desired.

Instead, it would be wiser to simply

(1) Isolate the VALUES clause. THIS could be done with a regular expression:

(?i)(?<=VALUES\s*\()(?:(?:[^)]*)|(?<=')(?:(?:''|[^']|\))*)(?=')(?=\)))

This basically covers all the SQL rules. It states that to match
(case-insensitive), the string "VALUES" followed by one or more spaces and a
left parenthesis must precede the match (positive look-behind), that the
match must be followed by a single right parenthesis, and that the match may
contain any combination of either (a)no right parentheses, or (b) a single
quote followed by either (i) any character that is either a doubled single
quote (escaped single quote in SQL statement) or (ii) not a single quote, or
(iii) a right parenthesis, and that any of these three possible combinations
may be repeated 0 or more times. It asserts that this is followed by a
single quote, and that that entire match is followed by a single right
parenthesis. This is because a text value may contain single quotes that are
escaped (by doubling them), and may contain a right parenthesis, but is
always surrounded by single quotes. Numeric and DateTime values will not be
surrounded by single quotes, and will not contain parentheses or single
quotes.

In your example, this would result in:

'valone ','valtwo' , 'valthree'

(2) Get the matches from the resulting match string using:

(?i)(?:(?:(?<=')(?:''|[^',])*(?='))|(?<!')[^,']+(?!'))*

This again, covers all the possible SQL rules. It states that a match
consists of either (1) a value that must be preceded by a single quote and
followed by a single quote, having 0 or more sequences of either (a) a
doubled (escaped) single quote, or (b)a non-single-quote or comma, or (2)
any value that is preceded by a single quote, is not a single quote, and is
followed by a single quote (for numeric and datetime values).

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Jesse,
Thankyou for replying. Unfortunately, i cannot seem to get it to work
.... here is the code i'm using:

private void button1_Click(object sender, System.EventArgs e)
{
String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";
String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+*)'\s*\)";

Regex rex = new Regex(sRX);

foreach(Match m in rex.Matches(sSQL))
{
MessageBox.Show(m.Value);
}

}
Thanks again,
James.

Jesse Houwing wrote:
* pigeonrandle wrote, On 24-7-2006 21:43:
Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').

This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...

Cheers,
James Randle.

This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse

Jul 25 '06 #6
Kevin,
I admire your willingness to take some time to explain *everything*.
Thankyou from the deepest recesses of my furrowed brow!

Everyone give Kevin a slap on the back ;O)

TRRRRRRRRRRRRRRRRRRRRRH!
James Randle.

Kevin Spencer wrote:
Hi James,

I don't believe that Regular Expressions alone are your solution here. First
of all, you haven't thought about all of the possible combinations of values
that you might need to capture. For example, you example indicates that all
text columns will be used, and therefore, all values will be surrounded with
single quotes. But what if the column is a numeric or datetime type? There
will be no single quotes. In this case, both the values in the first set of
parentheses, the column names, will match as well as the values in the
VALUES clause. In fact, it is necessary to discriminate between values which
are contained within parentheses and values that are not. In addition, there
arequite a few possible combinations of characters that would have to be
accounted for only withing parentheses, and using capturing groups would
reduce the number of items in each match that were in any given group to 1.
Here are some of the possible combinations you would have to deal with:

('value'
( 'value'
( value
(value
,'value'
, 'value'
,value
, value
'value')
'value' )
value)
value )
,value)
, value)
, value )
,'value')
, 'value')
,'value' )
, 'value' )

And you would need to eliminate the extraneous punctuation, which would
necessitate using grouping or positive and negative lookarounds. Note that
half of these examples would match for the first set of parentheses, the
column names. And don't forget the syntax rules of SQL, which indicate that
a text value may contain single quotes, but that they must be escaped by
doubling, and it may also contain commas. A numeric or datetime value may
not contain either commas or single quotes (unless it is in european format,
in which case you would have to allow for commas).

Now, a kluge could be worked out by using a large number of patterns ORed
together with no grouping, but you're getting into territory there which
would cause the use of a single regular expression to be much less efficient
than desired.

Instead, it would be wiser to simply

(1) Isolate the VALUES clause. THIS could be done with a regular expression:

(?i)(?<=VALUES\s*\()(?:(?:[^)]*)|(?<=')(?:(?:''|[^']|\))*)(?=')(?=\)))

This basically covers all the SQL rules. It states that to match
(case-insensitive), the string "VALUES" followed by one or more spaces and a
left parenthesis must precede the match (positive look-behind), that the
match must be followed by a single right parenthesis, and that the match may
contain any combination of either (a)no right parentheses, or (b) a single
quote followed by either (i) any character that is either a doubled single
quote (escaped single quote in SQL statement) or (ii) not a single quote,or
(iii) a right parenthesis, and that any of these three possible combinations
may be repeated 0 or more times. It asserts that this is followed by a
single quote, and that that entire match is followed by a single right
parenthesis. This is because a text value may contain single quotes that are
escaped (by doubling them), and may contain a right parenthesis, but is
always surrounded by single quotes. Numeric and DateTime values will not be
surrounded by single quotes, and will not contain parentheses or single
quotes.

In your example, this would result in:

'valone ','valtwo' , 'valthree'

(2) Get the matches from the resulting match string using:

(?i)(?:(?:(?<=')(?:''|[^',])*(?='))|(?<!')[^,']+(?!'))*

This again, covers all the possible SQL rules. It states that a match
consists of either (1) a value that must be preceded by a single quote and
followed by a single quote, having 0 or more sequences of either (a) a
doubled (escaped) single quote, or (b)a non-single-quote or comma, or (2)
any value that is preceded by a single quote, is not a single quote, and is
followed by a single quote (for numeric and datetime values).

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.
"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Jesse,
Thankyou for replying. Unfortunately, i cannot seem to get it to work
... here is the code i'm using:

private void button1_Click(object sender, System.EventArgs e)
{
String sSQL = @"INSERT INTO tblTest (colone, coltwo, colthree) VALUES
('valone ','valtwo' , 'valthree')";
String sRX =
@"values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+*)'\s*\)";

Regex rex = new Regex(sRX);

foreach(Match m in rex.Matches(sSQL))
{
MessageBox.Show(m.Value);
}

}
Thanks again,
James.

Jesse Houwing wrote:
* pigeonrandle wrote, On 24-7-2006 21:43:
Hello,
Does anyone know what RegEx i can use to extract the 'values' from an
INSERT INTO ... VALUES ('one','two','three').
>
This is obviously a simple example because a value might have a -' <-
in it.
I'm not sure how to get RegEx to ignore things...
>
Cheers,
James Randle.
>
This should about do it:

values\s*\(\s*(?:'(?<value>(?:[^']|'')+)'\s*,\s*)*'\s*(?<value>(?:[^']|'')+)'\s*\)

It puts all values in the 'value' named group.

You might have to do some extra work to get around newlines...
Jesse
Jul 25 '06 #7

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

Similar topics

0
1555
by: Gregory (Grisha) Trubetskoy | last post by:
My humble $0.02: From http://www.python.org/doc/current/lib/optparse-terminology.html: The phrase "required option" is an oxymoron; the presence of "required options" in a program is usually a...
1
2601
by: Bennett Haselton | last post by:
I want to get an ASP.Net hosting account with my ISP, and I'm trying to find out what level of access to the server is requried in order for me to view the server in Server Explorer in Visual...
5
2982
by: Lorenzo Bolognini | last post by:
Hi all, i need to detect whether a field is required or not. I'm using this code for building a string to convert later to an array (by Split) of which each element matches the field index (ex....
6
6758
by: yzzzzz | last post by:
Hi, In which cases is the <?xml version="1.0" encoding="UTF-8"?> processing instruction required at the beginning of an XML document, for the document to be valid? e.g. does it depend on the...
16
8446
by: Georges Heinesch | last post by:
Hi. My form contains a control (cboFooBar), which has an underlying field with the "Required" property set to "Yes". Now, while filling out all the controls of the form, I have to fill out this...
2
39066
by: bufbec1 | last post by:
I am pretty good with Access, but do not understand VBA. I have researched this topic and see only VBA answers, so I hope someone can help with my specific question. I have 2 fields for an...
3
2919
by: Orchid | last post by:
Hello All, Hope someone can help me on my required field problems. I have a form base on a table for users to input new Employees. There are 4 fields that cannot be Null when entering new...
3
5313
by: CindyRob | last post by:
I am using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, with hotfix 82202, Visual studio .NET 2003 with hotfix 823639. I have generated a proxy class using wsdl.exe from a schema that has an...
11
3975
by: Naeem | last post by:
I have a Javascript function, which changes a text field of a form into a select field. Following is the function function changeStateField() { var myForm =...
1
1542
by: KMEscherich | last post by:
Hi there, am wondering if there is a way to have this code capture 2 dates. You see, I have several fields and some are REQUIRED fields and some are NON-REQUIRED fields. I am attempting to capture...
0
7012
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...
0
7180
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,...
0
7225
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...
0
7392
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
5479
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,...
0
3105
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
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.