N Deepak Prasath
N Deepak Prasath

Reputation: 385

Extract between two words in a string that has newlines

I have a String like :

"Hey I am Peter I work as a Software Engineer. I know Java and I am new to RegEx".

I want to extract all characters from "Peter to new".

From gskinner.com/RegExr/ I can see that this pattern works - (?<=Hey)([^\n]*)(?=Reg)

But in Java, it does not work. Can any one please help ? What differently needs to be done in Java ?

My code looks like this:

    Pattern p = Pattern.compile("(?<=Hey)([^\n]*)(?=Reg)");
    Matcher m = p.matcher(p);
    m.find();
    System.out.println(m.group(0));

When I run, I get an Exception which says "No match Found".

Exception in thread "main" java.lang.IllegalStateException: No match found at java.util.regex.Matcher.group(Matcher.java:485)

Upvotes: 1

Views: 2794

Answers (3)

Bohemian
Bohemian

Reputation: 425418

You can do it all in one line:

String middle = str.replaceAll(".*(?<=Hey)([^\n]*)(?=Reg).*", "$1");

Here's a test:

String str = "Hey I am Peter I work as a Software Engineer. I know Java and I am new to RegEx";
String middle = str.replaceAll(".*(?<=Hey)([^\n]*)(?=Reg).*", "$1");
System.out.println(middle);

Output:

I am Peter I work as a Software Engineer. I know Java and I am new to 

Note that you get the same results with this regex

String middle = str.replaceAll(".*(?<=Hey)(.*)(?=Reg).*", "$1");

Your regex is looking for a string of characters, all of which are not newlines.

Upvotes: 0

davidfmatheson
davidfmatheson

Reputation: 3567

I am assuming that you have a newline somewhere in your string to match, something like:

String stringWithPeter = "Hey I am Peter I work as a Software Engineer." +
    System.getProperty("line.separator") + "I know Java and I am new to RegEx";

And that you want to match across this newline. You can tell Java to include newlines when matching dots ("."):

Pattern p = Pattern.compile("(?<=Hey)(.*)(?=Reg)", Pattern.DOTALL);

Then you will get the behavior you're looking for. I also found that when this worked:

String stringWithPeter = "Hey I am Peter I work as a Software Engineer.\nI know Java and I am new to RegEx";
Pattern p = Pattern.compile("(?<=Hey)([^\r]*)(?=Reg)");

But I couldn't tell you why, exactly.

Upvotes: 5

Adrian Wragg
Adrian Wragg

Reputation: 7411

You have left an unescaped \n in your expression, which is being put into the string as a newline character.

Try:

    Pattern p = Pattern.compile("(?<=Hey)([^\\n]*)(?=Reg)");
    Matcher m = p.matcher(stringToMatch);
    m.find();
    System.out.println(m.group(0));

instead.

[Credit also to user Explosion Pills for noticing the parameter to p.matcher was also incorrect]

Upvotes: 1

Related Questions