By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Regular Expressions Multiple Lines

P: n/a
I've been trying to figure out this for many hours now. I have a file
that is in the following format:

Game #xxxxxxxxx
stuff
stuff
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
stuff
-----------------------------

Here is my code:

//Match from 'Game # to ----------------------------- so includes all
the stuff.
string sRegex = @"Game\s#\.+-----------------------------";

MatchCollection theMatches = Regex.Matches(sText, sRegex,
RegexOptions.Singleline);

where, sText is the above text.

I then want to do:

foreach( Match m in theMatches)
{
string s = m.ToString();
//Do other text parsing
}

I want there to be three matches. So that the string s contains
everything from Game to the line of hyphens (------------------). But
this regex finds only one match and it is the entire string all three
segments. Any ideas on what a regular expression that would work.
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
..+

matches as many characters as possible. Try using a non-greedy match
instead:

..+?

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Jeremy R. Harner" <jr******@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I've been trying to figure out this for many hours now. I have a file
that is in the following format:

Game #xxxxxxxxx
stuff
stuff
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
stuff
-----------------------------

Here is my code:

//Match from 'Game # to ----------------------------- so includes all
the stuff.
string sRegex = @"Game\s#\.+-----------------------------";

MatchCollection theMatches = Regex.Matches(sText, sRegex,
RegexOptions.Singleline);

where, sText is the above text.

I then want to do:

foreach( Match m in theMatches)
{
string s = m.ToString();
//Do other text parsing
}

I want there to be three matches. So that the string s contains
everything from Game to the line of hyphens (------------------). But
this regex finds only one match and it is the entire string all three
segments. Any ideas on what a regular expression that would work.

Nov 15 '05 #2

P: n/a
and dont forget to use the RegexOptions.Multiline flag so that [.*] will
match newline characters
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Eric Gunnerson [MS]" <er****@online.microsoft.com> wrote in message
news:eY**************@TK2MSFTNGP11.phx.gbl...
.+

matches as many characters as possible. Try using a non-greedy match
instead:

.+?

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "Jeremy R. Harner" <jr******@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I've been trying to figure out this for many hours now. I have a file
that is in the following format:

Game #xxxxxxxxx
stuff
stuff
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
-----------------------------
Game #xxxxxxxxx
stuff
stuff
stuff
stuff
-----------------------------

Here is my code:

//Match from 'Game # to ----------------------------- so includes all
the stuff.
string sRegex = @"Game\s#\.+-----------------------------";

MatchCollection theMatches = Regex.Matches(sText, sRegex,
RegexOptions.Singleline);

where, sText is the above text.

I then want to do:

foreach( Match m in theMatches)
{
string s = m.ToString();
//Do other text parsing
}

I want there to be three matches. So that the string s contains
everything from Game to the line of hyphens (------------------). But
this regex finds only one match and it is the entire string all three
segments. Any ideas on what a regular expression that would work.


Nov 15 '05 #3

P: n/a
I could be wrong, but I thought I wanted the RegexOption.Singleline
set so .+ will match every line. What would having the
RegexOption.Multiline option do. The documentation says:

Singleline Specifies single-line mode.

Changes the meaning of the dot (.) so it matches every character
(instead of every character except\n).

I tried this regular expression:

@"Game\s#.*?--------------" but it still returns no matches. Again i
have the SingleLine option set.
Nov 15 '05 #4

P: n/a
because the . in SingleLine mode will NOT match newline characters... what
i'm saying is it seems like you actually DO want the Multiline set...
"Jeremy R. Harner" <jr******@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I could be wrong, but I thought I wanted the RegexOption.Singleline
set so .+ will match every line. What would having the
RegexOption.Multiline option do. The documentation says:

Singleline Specifies single-line mode.

Changes the meaning of the dot (.) so it matches every character
(instead of every character except\n).

I tried this regular expression:

@"Game\s#.*?--------------" but it still returns no matches. Again i
have the SingleLine option set.

Nov 15 '05 #5

P: n/a
Hi,
[inline]
"Jeremy R. Harner" <jr******@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I could be wrong, but I thought I wanted the RegexOption.Singleline
set so .+ will match every line. What would having the
RegexOption.Multiline option do. The documentation says:

Singleline Specifies single-line mode.

Changes the meaning of the dot (.) so it matches every character
(instead of every character except\n).

I tried this regular expression:

@"Game\s#.*?--------------" but it still returns no matches. Again i
Are you sure you output each matched string because this should work fine.
The syntax is good and you use the SingleLine option, which is a must.

(Multiline option isn't required, it changes the behaviour of $^ so that it
matches every line instead of the whole text)

HTH
greetings

have the SingleLine option set.

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.