Advertisement

02.02.2008 at 11:30AM PST, ID: 23132251
[x]
Attachment Details

How to make C# Regex work with "new line" characters in textfile?

Asked by sapbucket in C# Programming Language, Regular Expressions

Hello,

NOTE: I am a C# programmer - if you give a solution with UNIX or PERL formatting I won't know what to do with it. Please observe the forum I am asking this question in (C#). Thanks!

 I am writing a parsing module using regex's and ran across a snag: when my input contains new lines characters my regex stops working. I think best explaination is with code:



        [Test]
        public void ApplyStartAndStopAnchors()
        {
            string urlContent = "abcdefghijklmnopqrstuvwxyz";
            string regexString = "mno";
            string regexStartString = "def";
            string regexStopString = "stu";

            RegexGenerator generator =
                new RegexGenerator(new List<string>(), regexString, regexStartString, regexStopString, urlContent);

            generator.ApplyStartAndStopAnchors();
            Assert.AreEqual("ghijklmnopqr", generator.UrlContent); // PASSES
        }

And this example wouldn't be complete without generator.ApplyStartAndStopAnchors() (this being where I need your help)

        public void ApplyStartAndStopAnchors()
        {
            string subRegex = @"(?<=" + _regexStartString + ")" + "(.+)" + "(?=" + _regexStopString + ")";

            Regex r = new Regex(subRegex);

            Match m = r.Match(UrlContent);

            if (m.Success)
                UrlContent = m.Groups[1].Value;
        }


Now here's the catch, if I use multiline input:

Input text file: (MultiLineRegexTest.txt)
<start>
abcdefghij
klmnop
qrstuvw
xyz
<stop>

Which is just the English alphabet spread across 4 lines.

Now, when I read this input text file it contains new line characters. Here is a test showing the result:

        [Test]
        public void ApplyStartAndStopAnchorsOnTestFileWithMultipleLines()
        {
            FileStream fs = FileSupport.OpenFile(@"../../../Testfiles/MultiLineRegexTest.txt");
            string content = FileSupport.GetTextFileAsString(fs);

            string regexString = "mno";
            string regexStartString = "def";
            string regexStopString = "stu";

            RegexGenerator generator =
                new RegexGenerator(new List<string>(), regexString, regexStartString, regexStopString, content);

            generator.ApplyStartAndStopAnchors();
            Assert.AreEqual("ghijklmnopqr", generator.UrlContent); // FAILS
        }

And the output of the failing Assert.AreEqual statement is:

<start>
RegexGeneratorTestFixture.ApplyStartAndStopAnchorsOnTestFileWithMultipleLines : FailedNUnit.Framework.AssertionException:
String lengths differ.  Expected length=12, but was length=32.
Strings differ at index 0.

expected:<"ghijklmnopqr">
 but was:<"abcdefghij\r\nklmnop\r\nqrstuvw\r\nxyz">
-----------^
<stop>



Notice \r\n in the "but was" output? Those are the new line characters. They make it so my Regex in ApplyStartAndStopAnchors() doesn't work anymore.


Does anyone know how to fix this?

Much thanks,
sapbucketStart Free Trial
[+][-]02.02.2008 at 11:41AM PST, ID: 20805581

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 11:43AM PST, ID: 20805586

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 11:47AM PST, ID: 20805602

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 11:47AM PST, ID: 20805604

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 11:52AM PST, ID: 20805628

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 01:13PM PST, ID: 20805941

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Regular Expressions
Sign Up Now!
Solution Provided By: b0lsc0tt
Participating Experts: 1
Solution Grade: A
 
 
[+][-]02.04.2008 at 10:50PM PST, ID: 20821125

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.06.2008 at 02:46PM PST, ID: 20836554

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.06.2008 at 03:14PM PST, ID: 20836746

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628