Regular expression

G

Guest

Hi everybody,

I have a very stupid question here but I can't get it to work.

string repText = @"abc\nabc";
Regex reg = new Regex(@"abc\nabc",RegexOptions.Singleline );
Match match = reg.Match(repText);

Why this doesn't match ?

I need to keep the Singleline because in my scenario, this is in the midlle
of the file.
in reallity what i want it's to extract abc\nabc in

blalballbalblalabl
asdfafasdfsdaf
abc
abc
asdfadfafadsfafs
adfadsfasdfasdfasdf

How can I get that to work with the singlefile options ?

thanks.
 
K

KBuser

I'm pretty sure all that Singleline does is makes it so the " . "
metacharacter will match \n (new lines).
 
J

jeremiah johnson

This works:

String test = "abc" + Environment.NewLine + "abc";

Regex r = new Regex(
@"abc" + Environment.NewLine + "abc",
RegexOptions.Singleline);

Match match = r.Match(test);

if (match.Success) {
Console.WriteLine("yay it worked");
}

Perhaps you are mixing up "\n" with your system newline... I dunno. If
the text file you have been trying to match again is generated on
windows (with windows newlines) this should work.

hope this helps a little.

jeremiah
 
G

Guest

hooooo that is true.

Forgot that in windows it's \r\n and not \n. \n only is for unix....

thanks

i'll try that tomorrow.

thanks

Joe.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top