0

I know this has been discussed many times, but I've tried so many methods of getting my regex to read info from multiple lines and none works. Here is what I am trying to do:

I have a text file. Inside of it reads:

Description1 This is a test description.  Yay.  Description2

The regex (?<=Description1\\s).*(?=\\sDescription2) works as intended in returning all text between, but not including the words Description1 and Description2.

However, I need to be able to do the same, even if the text in between is separated by another line. like so.

Description1 This is a 
test description.  Yay.  Description2

I've tried everything I can find, from the Pattern.MULTILINE flag, to Pattern.DOTALL, to adding (?s) to the beginning, to adding [/r/n], nothing seems to work.
Any helps would be appreciated. This is a small step to a much bigger personal project.

2 Answers 2

3

This should work

(?s)(?<=Description1\\s).*?(?=\\sDescription2)
 ^                       ^            

(?s) would make . match newlines

.*? would match lazily..i.e it would match as less as possible..This is because with .* you would end matching till the last Decription2

Sign up to request clarification or add additional context in comments.

Comments

2

Pattern.DOTALL seems to do what you want. Also as Anirudh mentioned in his answer (+1 for him) you may want to make .* reluctant by adding ? after it.

String input = "Description1 This is a \ntest description.  Yay.  Description2";
Pattern p = Pattern.compile("(?<=Description1\\s).*?(?=\\sDescription2)", Pattern.DOTALL);
Matcher m = p.matcher(input);
while (m.find()) {
    System.out.println(m.group());
}

output:

This is a 
test description.  Yay. 

If you dont like using Pattern.DOTALL flag instead of . you can use [\\s\\S] which will match any white space and non white space character. Same rule goes with other class characters like [\\d\\D]

4 Comments

I'm afraid none of these have worked, though I have a suspicion as to why. Remember I'm not reading text from an in-code variable, I'm reading it from a separate text file. Could that maybe mess with what is considered a line break and have to be approached differently?
@user2835796 there may be many possible reasons why your code fails but we only could guess what it is unless you post code you are using to read data from file and how you use regex on it.
Unfortunatly I am a brand new user and I cannot add an Answer to post the code for 7 hours, and the code is too long for a comment =/ I appreciate the help anyway.
@user2835796 Don't add code as answer but add it in your question. You can do it with edit option placed under your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.