在这两种情况下,我都使用零断言前瞻,(?=^[^\\])
以确保下一行继续具有我正在寻找的内容。
(?=
启动零断言前瞻,这需要该值存在但不消耗该值
^[^\\]
匹配一行的开头,后跟任何字符,然后是\
)
关闭断言
第1部分
这将匹配第 1 部分的所有文本,其中捕获的第一行后跟任意数量的带有\
.
^([^\\].*?)(?=^[^\\])
在 Debuggex 上实时编辑
Java Code Example:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "STARTFirstText blah, blah
\ 1next line with more text, but the leading backslash
\ 2next line with more text, but the leading backslash
\ 3next line with more text, but the leading backslash
STARTsecondText blah, blah
\ 4next line with more text, but the leading backslash
\ 5next line with more text, but the leading backslash
\ 6next line with more text, but the leading backslash
foo";
Pattern re = Pattern.compile("^([^\\\\].*?)(?=^[^\\\\])",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
$matches Array:
(
[0] => Array
(
[0] => STARTFirstText blah, blah
\ 1next line with more text, but the leading backslash
\ 2next line with more text, but the leading backslash
\ 3next line with more text, but the leading backslash
[1] => STARTsecondText blah, blah
\ 4next line with more text, but the leading backslash
\ 5next line with more text, but the leading backslash
\ 6next line with more text, but the leading backslash
)
[1] => Array
(
[0] => STARTFirstText blah, blah
\ 1next line with more text, but the leading backslash
\ 2next line with more text, but the leading backslash
\ 3next line with more text, but the leading backslash
[1] => STARTsecondText blah, blah
\ 4next line with more text, but the leading backslash
\ 5next line with more text, but the leading backslash
\ 6next line with more text, but the leading backslash
)
)
第2部分
这将匹配第一行,然后是几行以数字开头的行
^([^\d].*?)(?=^[^\d])
在 Debuggex 上实时编辑
例子
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "First you will see the following links for the items:
1111 leading 4 digits and then some text
2565 leading 4 digits and then some text
8978 leading 4 digits and then some text
Second you will see the following links for the items:
2222 leading 4 digits and then some text
3333 leading 4 digits and then some text
4444 leading 4 digits and then some text";
Pattern re = Pattern.compile("^([^\\d].*?)(?=^[^\\d])",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
$matches Array:
(
[0] => Array
(
[0] => First you will see the following links for the items:
1111 leading 4 digits and then some text
2565 leading 4 digits and then some text
8978 leading 4 digits and then some text
[1] =>
)
[1] => Array
(
[0] => First you will see the following links for the items:
1111 leading 4 digits and then some text
2565 leading 4 digits and then some text
8978 leading 4 digits and then some text
[1] =>
)
)