Reputation: 2001
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?
Upvotes: 1
Views: 570
Reputation: 367
which will make some sense i think,
Pattern pattern = Pattern.compile("^(\\*[^*].+)");
Upvotes: 1
Reputation: 89639
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)
Upvotes: 3