Regular Expressions Multiple Lines

  • Thread starter Thread starter Jeremy R. Harner
  • Start date Start date
J

Jeremy R. Harner

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.
 
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/
(e-mail address removed)-software.com [remove the first "CC."]
 
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.
 
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...
 
Hi,
[inline]
Jeremy R. Harner said:
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
 
Back
Top