473,851 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Regular Expressions

Hi!
There is a SQL Create Table statement, for example:

CREATE TABLE [dbo].[EM5Auftrag] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[Nummer] [int] NOT NULL,
[Auftrag] [varchar] ( 15) NOT NULL
.....
) ON [PRIMARY]

The Syntax isn't always the same. But it's always a correct SQL-Syntax.
For example the [ and ] are optionally or the new lines are on other
places, no tabs and so on.
I have to write a parser, which is able to find all attributes and
their properties (id, int, identity (1,1), not null). For each of this
attributes I need an Object (the class exists).

How can I find this Attributes. I tried it with a Regular Expression as

@"create table \[?dbo\]?.\[?" + Tabname + @"\]? \([!!!!!]\)"
!!!!! is the placeholder, I don't know what to write for it. Tabname is
the name of the Table. It's known.

What should I write in, for !!!!!???
If a solution with regex isn't possible, I need to find another way.
Thanks a lot!

Nov 17 '05 #1
10 2900
Regular expressions will solve your problem but you need to be more specific
about what you want and how you use them. Personally I would use multiple
regular expressions here. 1 to pull ou the contents of the Create (i.e.
between ( and ) in the stored proc) and a second to retrieve details for each
line.

Something like this:

1st regex:
@"(?imsn)cre ate table (\[?dbo\]?.\[?EM5Auftrag\]? ?\()(?<Contents >.*)\) ?on"

I know there is a .* in there and that in general .* is inefficient but in
that case it's best as there are not many characters after the final )

Pull out the contents of the group name "Contents" and then run the second
regex on it.

2nd Regex
@"(?imsn)(?<Lin e>((?!,\s\[?[a-zA-Z]).)*)"

This second regex might be a bit iffy but going on your examplp stored proc
it's the best I could come up with..

it pulls out each line of the stored proc. After that you can easily parse
out the type useing string parsing or another regex.

Hope this helps.
Brian Delahunty
Ireland

http://briandela.com/blog
"hc******@hotma il.com" wrote:
Hi!
There is a SQL Create Table statement, for example:

CREATE TABLE [dbo].[EM5Auftrag] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[Nummer] [int] NOT NULL,
[Auftrag] [varchar] ( 15) NOT NULL
.....
) ON [PRIMARY]

The Syntax isn't always the same. But it's always a correct SQL-Syntax.
For example the [ and ] are optionally or the new lines are on other
places, no tabs and so on.
I have to write a parser, which is able to find all attributes and
their properties (id, int, identity (1,1), not null). For each of this
attributes I need an Object (the class exists).

How can I find this Attributes. I tried it with a Regular Expression as

@"create table \[?dbo\]?.\[?" + Tabname + @"\]? \([!!!!!]\)"
!!!!! is the placeholder, I don't know what to write for it. Tabname is
the name of the Table. It's known.

What should I write in, for !!!!!???
If a solution with regex isn't possible, I need to find another way.
Thanks a lot!

Nov 17 '05 #2
What does this one mean: <content> or <line>?? I never saw this.
And how could I access one Line? Both RegEx are giving the same result
in my test.

pattern = @"(?imsn)cre ate table (\[?dbo\]?.\[?"+Tabname+@ "\]?
?\()(?<Contents >.*)\) ?on";
regEx = new Regex(pattern, RegexOptions.Ig noreCase);
m = regEx.Match(con tent);
if(m.Success){
//Console.Write(m .ToString());
pattern = @"(?imsn)(?<Lin e>((?!,\s\[?[a-*zA-Z]).)*)" ;
Regex regExAtt= new Regex(pattern, RegexOptions.Ig noreCase);
Match ma = regExAtt.Match( m.ToString());
if(ma.Success){
Console.Write(m a.Groups[1].Value);
}
}else{
}

Nov 17 '05 #3
They are named captures.

e.g.

string storedProc = <your stored proc text would be here>;
string pattern = @"(?imsn)cre ate table (\[?dbo\]?.\[?"+Tabname+@ "\]?
?\()(?<Contents >.*)\) ?on";

Match match = Regex.Match(sto redProc, pattern);

if(match.Succes s == true)
{
string content = match.Groups["Contents"].Value;

string secondPattern = @"(?imsn)(?<Lin e>((?!,\s\[?[a-¬*zA-Z]).)*)" ;

MatchCollection matches = Regex.Matches(c ontent, secondPattern);

foreach(Match m in matches)
{
string line = m.Groups[Line].Value;
// Do what you want with the line... e.g. additional parsing etc
}
}
You don't need to use an RegexOptions as the (?imsn) at the beginning of the
regex sets those.

Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog
"Crispie" wrote:
What does this one mean: <content> or <line>?? I never saw this.
And how could I access one Line? Both RegEx are giving the same result
in my test.

pattern = @"(?imsn)cre ate table (\[?dbo\]?.\[?"+Tabname+@ "\]?
?\()(?<Contents >.*)\) ?on";
regEx = new Regex(pattern, RegexOptions.Ig noreCase);
m = regEx.Match(con tent);
if(m.Success){
//Console.Write(m .ToString());
pattern = @"(?imsn)(?<Lin e>((?!,\s\[?[a-¬*zA-Z]).)*)" ;
Regex regExAtt= new Regex(pattern, RegexOptions.Ig noreCase);
Match ma = regExAtt.Match( m.ToString());
if(ma.Success){
Console.Write(m a.Groups[1].Value);
}
}else{
}

Nov 17 '05 #4
The problem I have, is that i can't access one line. in the
ma.Groups["Line"].Value is the hole content...

pattern = @"(?imsn)cre ate table (\[?dbo\]?.\[?"+Tabname+@ "\]?
?\()(?<Contents >.*)\) ?on";
m = Regex.Match(con tent, pattern);
contentCreate = m.Groups["Contents"].Value;
if(m.Success){
string secondPattern =
@"(?imsn)(?<Lin e>((?!,\s\[?[a-**zA-Z]).)*)" ;
MatchCollection matches = Regex.Matches(c ontentCreate,
secondPattern);
foreach(Match ma in matches) {
string line = ma.Groups["Line"].Value;
Console.WriteLi ne("test:" + line);
// Do what you want with the line... e.g. additional parsing
etc
}
}

RESULT:
test:
[ID] [int] IDENTITY (1, 1) NOT NULL,
[Nummer] [int] NOT NULL,
[Auftrag] [varchar] ( 15) NOT NULL,

test:

And not what I want:
test [ID] [int] IDENTITY (1, 1) NOT NULL,
test [Nummer] [int] NOT NULL,
.....

Nov 17 '05 #5
Yes. This is probably due to the fact that I based my regex on what was
posted here (which was: "CREATE TABLE [dbo].[EM5Auftrag] ( [ID] [int]
IDENTITY (1, 1) NOT NULL, [Nummer] [int] NOT NULL, [Auftrag] [varchar] ( 15)
NOT NULL ..... ) ON [PRIMARY] ") and which obviously will not match what is
in memory when you pull back the stored proc from whereever you are storing
it. My regex's are only meant to be a starting point... not a solution...
sorry if I didn't make that clear.

Try this regex as the second one:

@"(?imsn)[^\[a-zA-z]+(?<Line>((?!,\ n).)*)";

That should work

Hope this helps!

Brian Delahunty
Ireland

http://briandela.com/blog
"Crispie" wrote:
The problem I have, is that i can't access one line. in the
ma.Groups["Line"].Value is the hole content...

pattern = @"(?imsn)cre ate table (\[?dbo\]?.\[?"+Tabname+@ "\]?
?\()(?<Contents >.*)\) ?on";
m = Regex.Match(con tent, pattern);
contentCreate = m.Groups["Contents"].Value;
if(m.Success){
string secondPattern =
@"(?imsn)(?<Lin e>((?!,\s\[?[a-¬*¬*zA-Z]).)*)" ;
MatchCollection matches = Regex.Matches(c ontentCreate,
secondPattern);
foreach(Match ma in matches) {
string line = ma.Groups["Line"].Value;
Console.WriteLi ne("test:" + line);
// Do what you want with the line... e.g. additional parsing
etc
}
}

RESULT:
test:
[ID] [int] IDENTITY (1, 1) NOT NULL,
[Nummer] [int] NOT NULL,
[Auftrag] [varchar] ( 15) NOT NULL,

test:

And not what I want:
test [ID] [int] IDENTITY (1, 1) NOT NULL,
test [Nummer] [int] NOT NULL,
.....

Nov 17 '05 #6
I tried it with exactly the postet one...
The first Regex works fine, and i get the content from create to ...)
ON. This is what i need. Now I need this content not at one piece, but
on lines.
there are 16 lines with attributes in there... The problem is, that I
can't access this attributes separatly. In SQL, its possible to write
this attributes all 16 on the same line or as this every on a own line.
With this new secondRegex, it doesn't works. The result I get is as
following:
61times:
test:
test:
....
the variable "line" is empty.

if i try ma.value, i get only the numbers in the content.
[ID] [int] IDENTITY (1, 1) NOT NULL,
-->
test:
test:
test: (1, 1)
test:
test:,

I don't understand this result. why are all letters hidden?

I'm very sorry, I'm not experienced in programming with regex... I read
some documents, but didn't understand it well.
Greets and thanks for being patience.

Nov 17 '05 #7
@"(?imsn)[a-zA-z]+(?<Line>(*(?!, \n).)*)";

Deleting ^\[ in your solution gives me the result as follows:
test:[ID]
test:[int]
test:IDENTITY
test:NOT
test:NULL
test:[Nummer]
test:[int]
test:NOT
test:NULL

But now, there aren't any numbers... I need also the IDENTITY (1, 1) or
( 15) as the length of a varchar...
whats the problem?

Nov 17 '05 #8
Can you debug through your code and copy out the contents of "content"
variable just before you run the regex on it.. i.e. the stored procedure as
it appears in memory. Paste it here and I'll see if I can do up a regex for
it.
--
Brian Delahunty
Ireland

http://briandela.com/blog
"Crispie" wrote:
@"(?imsn)[a-zA-z]+(?<Line>(¬*(?! ,\n).)*)";

Deleting ^\[ in your solution gives me the result as follows:
test:[ID]
test:[int]
test:IDENTITY
test:NOT
test:NULL
test:[Nummer]
test:[int]
test:NOT
test:NULL

But now, there aren't any numbers... I need also the IDENTITY (1, 1) or
( 15) as the length of a varchar...
whats the problem?

Nov 17 '05 #9
I solved the problem:
I now take this one: @"(?imsn)[a-zA-z0-9(),
-_]+(?<Line>(*(?!, \n).)*)";
(added 0-9(),-_ into your regex)

the output now shows like this (ignore true/false, thats another test):
test:[ID] True False
test:[int] False True
test:IDENTITY (1, 1) False False
test:NOT NULL, False False
test:[Nummer] True False
test:[int] False True
test: False False
test:NOT NULL, False False
test:[Auftrag] True False
test:[varchar] False True
test:( 15) False False
test:NOT NULL, False False

So I have now every single item. Now I can create some
Attribute-Objects.

Thanks for help,
Greets

Nov 17 '05 #10

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

Similar topics

1
4193
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
2
5114
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I have to use all the expressions seperately? Here are my regular expressions that check for valid email address and link Dim Expression As String =
1
509
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I have to use all the expressions seperately? Here are my regular expressions that check for valid email address and link Dim Expression As String =
4
5197
by: EgyŁd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or adate LIKE "2004.01.10 __:15") .... into something like this: .... where adate LIKE "2004.01.10 __:(30/15)" ...
1
4392
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
0
9747
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
11017
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
10670
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...
0
10352
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...
0
9505
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, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7905
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
5736
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...
0
5933
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4548
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

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.