开发者

Regular Expression to stop after first find

if I have the following text:

some text
CREATE DATABASE [$(DatabaseName)]
This can be any text
GO

other text
GO

more text

I would like to only match

CREATE DATABASE [$(DatabaseName)]  
any text that is here
GO

using Regular Expression. What I have tried so far fails and gives me everything up to the last GO. I would like to stop after finding the first occurence of GO.

Thi开发者_高级运维s is what I tried:

CREATE DATABASE[^<>]*^(GO)+?

I understand why it does not work, but don't know how to fix it. Thanks a lot.


Append a ? to your regex to make it non greedy

http://www.lbsharp.com/wordpress/index.php/2007/02/19/net-regular-expressions/


This will do it:

CREATE DATABASE \[(.+?)\]


As I understand it, this works for me (where dot matches new line is set, if necessary)

CREATE DATABASE.*?GO

See it tested at rubular.com with this permalink I created for it: http://rubular.com/r/2KsBrnSGhC


I'm terrible with regex, so if you're in a .Net environment, try using this great addin from MS:

http://visualstudiogallery.msdn.microsoft.com/en-us/55c24bf1-2636-4f94-831d-28db8505ce00


I found how to do it:

CREATE DATABASE+([\s\S]*?)+GO

or still shorter

CREATE DATABASE[\s\S]*?GO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜