0

I need a regular expression to match the very first word within the following source:

WanRoutingProtocol=
     Static



              192.160.22.0/27
              false

           2004:BA2:78::50


  =IAS

I just want to extract the very first word (In this case "Static") using a regular expression in java.

The blank lines contains multiple newlines.

I'm using the following regex

  "^(\\n)+Static.*IAS"

but this is not working.

Filburt
  • 17,626
  • 12
  • 64
  • 115
user1356042
  • 395
  • 2
  • 6
  • 23

1 Answers1

1

Use the following regex. Expression assumes that the input would always start and end with the keywords "WanRoutingProtocol" and "IAS" and would fetch any keyword present at the place of "Static".

^WanRoutingProtocol=\\s*(.*)[\\s\\w\\./:]*=IAS$

Here's how you could do this in Java. (There's no need to use Pattern.MULTILINE)

String input = "WanRoutingProtocol=\n" +
    "     Static\n" +
    "\n" +
    "\n" +
    "\n" +
    "              192.160.22.0/27\n" +
    "              false\n" + 
    "\n" +
    "           2004:BA2:78::50\n" +
    "\n" +
    "\n" +
    "  =IAS";
Pattern p = Pattern.compile("^WanRoutingProtocol=\\s*(.*)[\\s\\w\\./:]*=IAS$");
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println(m.group(1)); // prints "Static"
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89