Reputation: 2101
How can I get
Text t;
Graph g;
from
// Co
Text t;
Graph g;
// Co
?
// Co\r?\n(.*)\r?\n// Co
works when there is one line, but does not work with multiple lines.
Upvotes: 2
Views: 200
Reputation: 183602
By default, .
matches any character except newlines (\n
), but you can use the Pattern.DOTALL
flag to make it match newlines as well. So, for example, instead of this:
final Pattern pat = Pattern.compile("// Co\r?\n(.*)\r?\n// Co");
you would use this:
final Pattern pat = Pattern.compile("// Co\r?\n(.*)\r?\n// Co", Pattern.DOTALL);
Equivalently, you can set the s
flag inside the regex itself, using either of these styles:
final Pattern pat = Pattern.compile("// Co\r?\n((?s).*)\r?\n// Co");
final Pattern pat = Pattern.compile("// Co\r?\n(?s:(.*))\r?\n// Co");
which lets you control exactly which part of the regex accepts a newline for .
. (But I'd stick with the DOTALL
approach unless you have a regex where .
sometimes means the one thing, sometimes the other.)
Edited to add: It seems very likely — but you'll have to determine this for yourself, based on your use-case — that you need to use non-greedy quantification, .*?
, rather than greedy quantification, .*
.
Upvotes: 5