Avatar of orion7999
orion7999
 asked on

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.
C#

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
SOLUTION
Ravi Singh

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
orion7999

ASKER
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);
orion7999

ASKER
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.

Fernando Soto

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Fernando Soto

What version of .Net framework are you using?
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
orion7999

ASKER
this worked. great thanks.
Fernando Soto

Not a problem, glad I was able to help. ;=)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.