C#正则表达式 [英] C# Regular Expressions

查看:74
本文介绍了C#正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

有一个SQL Create Table语句,例如:

CREATE TABLE [dbo]。[EM5Auftrag](

[ID] [int] IDENTITY(1,1)NOT NULL,

[Nummer] [int] NOT NULL,

[Auftrag] [varchar] (15)NOT NULL

.....

)ON [PRIMARY]


语法并不总是相同。但它总是一个正确的SQL语法。

例如[和]是可选的或者新行是在其他

的地方,没有标签等等。

我必须编写一个解析器,它能够找到所有属性和

它们的属性(id,int,identity(1,1),not null)。对于每个

属性,我需要一个Object(该类存在)。


如何找到这个属性。我试用了正则表达式作为


@" create table \ [?dbo \]?。\ [? + Tabname + @" \]? \([!!!!!] \)"

!!!!!是占位符,我不知道该写些什么。 Tabname是表的名称。
表的名称。它是众所周知的。


我应该写什么,为!!!!! ???

如果正则表达式的解决方案不是可能,我需要找到另一种方式。

非常感谢!

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!

推荐答案

正则表达式将解决你的问题,但你需要更具体的

关于你想要什么以及如何使用它们。就个人而言,我会在这里使用多个

正则表达式。 1将你的内容(即存储过程中的(和)之间的

)和一秒内的内容拉出来检索每个

行的详细信息。


这样的事情:


第一个正则表达式:

@"(?imsn)创建表(\ [?dbo \]?。\ [?EM5Auftrag \]??\()(?< Contents>。*)\)?on"


我知道有a。*在那里和那个一般。*是低效的但是在

这种情况​​下它是最好的,因为在决赛后没有很多角色)


拉出组名内容的内容。然后运行第二个

正则表达式。


第二个正则表达式

@"(?imsn)(?<行>((?!,\\\\ [?[a-zA-Z])。)*)


第二个正则表达式可能有点不确定但是要去在你的examplp存储过程中

它是我能想到的最好的...


它会拉出存储过程的每一行。之后,您可以轻松地解析

类型使用字符串解析或其他正则表达式。


希望这会有所帮助。

Brian Delahunty

爱尔兰

http://briandela.com / blog

" hc ****** @ hotmail.com"写道:
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)create 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)(?<Line>((?!,\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******@hotmail.com" wrote:
嗨!
有一个SQL Create Table语句,例如:

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

语法并不总是一样的。但它总是一个正确的SQL语法。
例如[和]是可选的或新的行在其他地方,没有标签等等。
我必须编写一个解析器,它能够找到所有属性和它们的属性(id,int,identity(1,1),而不是null)。对于每个
属性,我需要一个Object(该类存在)。

如何找到这个属性。我用正则表达式试了一下

@" create table \ [?dbo \]?。\ [? + Tabname + @" \]? \([!!!!!] \)"
!!!!!是占位符,我不知道该写些什么。 Tabname是表的名称。它是众所周知的。

我应该写什么,为!!!!! ???
如果一个正则表达式的解决方案是不可能的,我需要找到另一种方式。
非常感谢!
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!



这是什么意思:< content>或< line> ??我从来没有见过这个。

我怎么能进入一条线?两个RegEx在我的测试中都给出了相同的结果




pattern = @"(?imsn)create table(\ [?dbo \ ]?。\ [?" + Tabname + @" \]?

?\()(?< Contents>。*)\)?on" ;;

regEx = new Regex(pattern,RegexOptions.IgnoreCase);

m = regEx.Match(content);

if(m.Success){

//Console.Write(m.ToString());

pattern = @"(?imsn)(?< Line>((?!,\ s) ?\ [[A- * ZA-Z]))*)" ;

正则表达式regExAtt =新的正则表达式(模式,RegexOptions.IgnoreCase);

匹配ma = regExAtt.Match(m.ToString());

if(ma.Success){

Console.Write(ma.Groups [1] .Value);

}

} else {

}

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)create table (\[?dbo\]?.\[?"+Tabname+@"\]?
?\()(?<Contents>.*)\) ?on";
regEx = new Regex(pattern, RegexOptions.IgnoreCase);
m = regEx.Match(content);
if(m.Success){
//Console.Write(m.ToString());
pattern = @"(?imsn)(?<Line>((?!,\s\[?[a-*zA-Z]).)*)" ;
Regex regExAtt= new Regex(pattern, RegexOptions.IgnoreCase);
Match ma = regExAtt.Match(m.ToString());
if(ma.Success){
Console.Write(ma.Groups[1].Value);
}
}else{
}


它们被命名为捕获。


例如


string storedProc =<你存储的proc文本将在这里> ;;

string pattern = @"(?imsn)create table(\ [?dbo \\ \\]?。\ [?" + Tabname + @" \]?

?\()(?< Contents>。*)\)?on";


匹配匹配= Regex.Match(storedProc,pattern);


if(match.Success == true)

{

string content = match.Groups [" Contents"]。Value;


string secondPattern = @"(?imsn)(?< ;线>((,\s\ [[一 - * ZA-Z])?!??)*)" ;


MatchCollection匹配= Regex.Matches(content,secondPattern);


foreach(在比赛中匹配m)

{

string line = m.Groups [Line] .Value;

//用线条做你想做的事......额外的解析等

}

}

你不需要在开头使用RegexOptions作为(?imsn)

正则表达式设置那些。


希望这会有所帮助。


-

Brian Delahunty

爱尔兰

http:// briandela .com / blog

" Crispie"写道:
They are named captures.

e.g.

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

Match match = Regex.Match(storedProc, pattern);

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

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

MatchCollection matches = Regex.Matches(content, 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:
这是什么意思:< content>或< line> ??我从来没有见过这个。
我怎么能访问一条线?两个RegEx在我的测试中都给出了相同的结果。

pattern = @"(?imsn)create table(\ [?dbo \]?。\ [? ; + Tabname + @" \]?
?\()(?< Contents>。*)\)?on" ;;
regEx = new Regex(pattern,RegexOptions.IgnoreCase) ;
m = regEx.Match(内容);
if(m.Success){
//Console.Write(m.ToString());
pattern = @" ;(?浸锡)(小于线>((,\s\ [[一 - * ZA-Z]))*?!?)" ;
正则表达式regExAtt =新的正则表达式(pattern,RegexOptions.IgnoreCase);
匹配ma = regExAtt.Match(m.ToString());
if(ma.Success){
Console.Write(ma.Groups [1] .Value);
}
} else {
}
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)create table (\[?dbo\]?.\[?"+Tabname+@"\]?
?\()(?<Contents>.*)\) ?on";
regEx = new Regex(pattern, RegexOptions.IgnoreCase);
m = regEx.Match(content);
if(m.Success){
//Console.Write(m.ToString());
pattern = @"(?imsn)(?<Line>((?!,\s\[?[a-?*zA-Z]).)*)" ;
Regex regExAtt= new Regex(pattern, RegexOptions.IgnoreCase);
Match ma = regExAtt.Match(m.ToString());
if(ma.Success){
Console.Write(ma.Groups[1].Value);
}
}else{
}



这篇关于C#正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆