Eric Ipsum
Eric Ipsum

Reputation: 745

regular expression to take two line

i have a line like:

ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.
Follow the given instruction below
instruction one.............

i want a regular expression which will cover

first two line that means from "ERROR" to "is not installed".

i use the below

(ERROR\:[^\.]+\.?)

but it takes only

ERROR: file' user\username\file\myfile

any kind of help will be appreciated, Thanks

Upvotes: 0

Views: 2714

Answers (3)

user1134181
user1134181

Reputation:

By using java.util.regex:

public static void main(String[] args) {
      String line = "ERROR: file' user\\username\\file\\myfile.mp3' c\n"+
                    "annot be used, because required software is not installed.\n\n"+
                    "Follow the given instruction below\n"+
                    "instruction one.............\n";

      String regex = "^ERROR: [A-Za-z0-9\\.,'\\s\\\\\n]+(?=\\s{2})";

      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(line);
      if(matcher.find()) {
         System.out.println(line);
         System.out.println(matcher.group(0) );
      }
}

You'll get:

ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.

Follow the given instruction below
instruction one.............

ERROR: file' user\username\file\myfile.mp3' c
annot be used, because required software is not installed.

For regex101.com:

^ERROR: [A-Za-z0-9\\.,'\s\\\n]+(?=\s{2})

See result here: https://regex101.com/r/sL8vQ3/1

Upvotes: 0

anubhava
anubhava

Reputation: 786291

You can use this regex:

\bERROR:[\s\S]*?\.(?=[\r\n])

RegEx Demo

It starts matching with text ERROR: and matches everything including newline characters until a DOT is found just before a newline.

Upvotes: 3

Whothehellisthat
Whothehellisthat

Reputation: 2152

If we can rely on the format of there being an empty line after the desired message, we could use that to capture the message itself:

/ERROR\:[\W\w]*?(?=\r?\n\r?\n)/

  • ERROR\: Literal text.
  • [\W\w]*? Matches any character zero or more times, but as few times as possible. This means it will match up to whatever should come next.
  • (?=\r?\n\r?\n) Matches if the following characters make up an empty line. (But doesn't include that text as part of the match itself.)

Upvotes: 1

Related Questions