C# Regular expression for matching text across multiple lines

I have the following text that i need to parse.

service xyz_1234
  ip address 111.111.111.111

  protocol tcp

  port 0000

  active

I need to match
service xyz_1234
  ip address 111.111.111.111
with a regular expression

I am having problem as the IP address is on a newline.

HEre is the regular expression I tried. It didnt work.

Regex.Match(str, @"service xyz_1234\s*\r\r\s*ip address\s*(?<ig>\w+\.\w+\.\w+\.\w+)");

Please let me know where I am going wrong.
orion7999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravi SinghSenior Software EngineerCommented:
Hi, try:

Regex.Match(str, @"service\sxyz_1234\s*ip\saddress\s*(?<ig>\d+\.\d+\.\d+.\d+)", RegexOptions.Multiline);
orion7999Author Commented:
hi...sxyz_1234 is a variable. It can keep changing. I tried this. it didnt work.

string ipexp = @"service " + osm.ServiceName + @"\s*ip\saddress\s*(?<ig>\d+\.\d+\.\d+.\d+)";
Match ipmatch = Regex.Match(str, ipexp, RegexOptions.Multiline);

I tried matching your statement directly like below too. It does not find the match.
Match ipmatch = Regex.Match(str, @"service\sxyz_1234\s*ip\saddress\s*(?<ig>\d+\.\d+\.\d+.\d+)", RegexOptions.Multiline);
orion7999Author Commented:
hi it works now...but i have a case when IP address need not be in the next line. The text could be in this format.

service xyz_1234
  protocol tcp

  port 0000

  active

 ip address 111.111.111.111


The regular expression you gave does not match in these cases.

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Fernando SotoRetiredCommented:
Hi orion7999;

Try this it will work for you.

      string input = "service xyz_1234\n  ip address 111.111.111.111\n\n  protocol tcp\n\n  port 0000\n\n  active";
      string service = "";
      string ip = "";
      Match m = Regex.Match(input, @"service\s+(?<Service>\w+_\d+).*?ip\s+address\s+(?<IP>\d+\.\d+\.\d+\.\d+)",
            RegexOptions.Singleline | RegexOptions.IgnoreCase);
      if( m.Success )
      {
            service = m.Groups["Service"].Value;
            ip = m.Groups["IP"].Value;
      }

Fernando
Fernando SotoRetiredCommented:
What version of .Net framework are you using?
Fernando SotoRetiredCommented:
Hi orion7999;

In re-reading through the post I noted that you say that the xyz_123 is a variable. You will need to escape that string before using it just in case a Regex meta-character is within the string. The following code is updated from my last post.

      string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)+";
      string serviceVar = Regex.Replace(xyz_123, escape, @"\$1");
      string input = "service " + serviceVar + "\n  ip address 111.111.111.111\n\n  protocol tcp\n\n  port 0000\n\n  active";
      string service = "";
      string ip = "";
      Match m = Regex.Match(input, @"service\s+(?<Service>\w+_\d+).*?ip\s+address\s+(?<IP>\d+\.\d+\.\d+\.\d+)",
            RegexOptions.Singleline | RegexOptions.IgnoreCase);
      if( m.Success )
      {
            service = m.Groups["Service"].Value;
            ip = m.Groups["IP"].Value;
      }

Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
orion7999Author Commented:
this worked. great thanks.
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help. ;=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.