[英]Java regular expression to capture repetive groups
I am not able to capture all the data including all chars ,digits, spaces and special characters from group 2 through regular expression 我无法通过正则表达式捕获第2组中的所有数据,包括所有字符,数字,空格和特殊字符
tried regular expressions 尝试过正则表达式
final String regex = "^:(.*?)//(.*[\\s\\S]?)";
String line1 = ":Humpty Dumpty sat on a wall";
String line2 = "//Humpty Dumpty had a great fall";
String rhyme = line1 + line2+"\n"+ "ssdsds"+"\n";
final String value = rhyme.replaceAll(regex , "$2");
final boolean formatIdentified = rhyme.matches(formatRegex);
System.out.println(formatIdentified);//returns false
value I am expecting 我期望的价值
"Humpty Dumpty had a great fall
ssdsds
"
corrected regular expression should work with format :abc//xxxx
, output should be xxxx
. 更正后的正则表达式应使用格式
:abc//xxxx
,输出应为xxxx
。
It is entirely possible that String.replaceAll
and String.matches
are handling the terminator for multi-line strings differently, ie newline vs end of the string, which is why value
may print the expected result but matches
prints false. String.replaceAll
和String.matches
完全有可能以不同的方式处理多行字符串的终止符,即换行符与字符串的结尾,这就是为什么value
可以打印预期结果但matches
结果显示false的原因。
I would be more explicit and use Pattern
and Matcher
directly rather than proxied through String: 我将更加明确,直接使用
Pattern
和Matcher
而不是通过String进行代理:
String rhyme =
":Humpty Dumpty sat on a wall\n" +
"//Humpty Dumpty had a great fall\n" +
"ssdsds\n";
Pattern pattern = Pattern.compile("^:(.*?)//(.*[\\s\\S]?)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(rhyme);
if (matcher.find()) {
System.out.println(matcher.group(2));
} else {
System.out.println("[Pattern not found]");
}
This will output: 这将输出:
Humpty Dumpty had a great fall
ssdsds
If you are expecting to only match up to a new line ending you just need to change the flag to Pattern.MULTILINE. 如果期望只匹配一个新行结尾,则只需将标志更改为Pattern.MULTILINE。
Pattern pattern = Pattern.compile("^:(.*?)//(.*[\\s\\S]?)", Pattern.MULTILINE);
Which will output: 将输出:
Humpty Dumpty had a great fall
This provides the output you wanted. 这将提供您想要的输出。
// ignore everything up to // and then include // and all following
// in capture group 1.
final String regex = ".*(//.*)";
String line1 = ":Humpty Dumpty sat on a wall";
String line2 = "//Humpty Dumpty had a great fall";
String rhyme = line1 + line2 + "\n" + "ssdsds" + "\n";
final String value = rhyme.replaceAll(regex, "$1");
System.out.println(value);
// or the follwing if you want the double quotes.
System.out.println("\"" + value + "\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.