How can I write a regex that will put the first line in a group, and then everything else in a second group?
views:
38answers:
4Even if you set `s` and `m` flags, the first `()` will match the entire string due to the greedy `*` and the fact that second group can match an empty string.
Amarghosh
2010-07-08 12:46:38
+1
A:
Since .
does not match newline characters if you do not set the dotmatchesall (or singleline) flag, you can simply do
(.*)\r\n([\s\S]*)
Note that \r\n
needs to be changed to whatever your files uses for its newline flag. That would be \n
on Linux or \r
on Macs (according to the Wikipedia).
Jens
2010-07-08 12:34:46
+2
A:
Depending on flavor, something like this should work: (?-s)(.*)(?s)(.*)
. This matches and captures (.*)
twice, the first with "single-line" mode switched off, and the second with it switched on. You may even want to anchor the whole pattern within \A
and \Z
.
In Java:
String text =
"first line\r" +
"second line\r\n" +
"third line etc";
System.out.println(
text.replaceFirst(
"(?-s)(.*)(?s)(.*)",
"FIRST <<<$1>>>\nREST <<<$2>>>"
)
);
This prints (as seen on ideone.com):
FIRST <<<first line>>>
REST <<<
second line
third line etc>>>
Depending on what you're actually doing (e.g. reading a file line by line), probably much better solution exists.
polygenelubricants
2010-07-08 12:37:09
The "nice" thing about this is that it doesn't assume what the line separator is. `java.util.regex.Pattern` recognizes many, including some Unicode ones that aren't just combinations of `\r` and `\n`.
polygenelubricants
2010-07-08 12:51:20