如何编写一个正则表达式,用于匹配多行由新行和空格定义的正则表达式?java正则表达式字符串匹配和多行用新行分隔
下面的代码适用于一个多但如果输入 是
String input = "A1234567890\nAAAAA\nwwwwwwww"
我的意思是matches()
不是输入真正不起作用。
这里是我的代码:
package patternreg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pattrenmatching {
public static void main(String[] args) {
String input = "A1234567890\nAAAAA";
String regex = ".*[\\w\\s\\w+].*";
Pattern p = Pattern.compile(regex,Pattern.MULTILINE);
Matcher m =p.matcher(input);
if (m.matches()) {
System.out.println("matches() found the pattern \""
+ "\" starting at index "
+ " and ending at index ");
} else {
System.out.println("matches() found nothing");
}
}
}
看起来像它的工作原理!给一些更具体的,会发生什么?应该发生什么? – Bhushan 2012-02-16 19:45:33
嗨Bhushan,匹配应返回匹配找到的模式,即使输入由换行符分隔假设如果输入有多个换行符A1234567890 \ nAAAAA \ ndddd \ ndddd \ nddd,匹配返回匹配()什么也没有发现“ – user1182067 2012-02-16 20:08:11
我复制并粘贴你的代码和执行,我得到这个输出:A1234567890 AAAAA * matches()发现模式“”从索引开始到索引 – Bhushan 2012-02-16 20:19:29