1

Here is a string

* '''A happy man is too satisfied with the present to dwell too much on the future.'''
** "My Future Plans" an essay written at age 17 for school exam (18 September 1896) ''The Collected Papers of Albert Einstein'' Vol. 1 (1987) Doc.22

I need to get the text which starts with only one * till the end of that line

Pattern pattern = Pattern.compile("\\*(.+?)\\n");
    Matcher matcher = pattern.matcher(text);
    List<String> s = new ArrayList<String>();
    while (matcher.find()) {
        s.add(matcher.group(1));
    }

I tried the above code, but it matches even 2 * and also the text from * to new line is not coming up properly, what am I missing here?

2 Answers 2

3

You can try this:

Pattern pattern = Pattern.compile("(?m)^\\*([^*].*)");

(?m) set the pattern in multiline mode where ^ means "begining of the line"

Adding \\n at the end is useless since the dot can't match a newline. (it's the reason why a lazy quantifier is useless too)

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

2 Comments

@Naren: The quantifiers * and + are greedy by default and take all possible characters. Since . doesn't match a newline (or the end of the string) .* will stop automaticaly before the new line.
@Naren: take a look at the site: www.regular-expressions.info to have an explanation about lazy/greedy quantifiers
1

which will make some sense i think,

Pattern pattern = Pattern.compile("^(\\*[^*].+)");

Comments

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.