0
特定字符串的字符串,我需要在字符串匹配/*exa*/mple*/
一个子正则表达式,正则表达式匹配不包含在Java中
匹配的字符串必须/*exa*/
不/*exa*/mple*/
。
它也不能包含其中的"*/"
。
我尝试了这些正则表达式:
"/\\*[.*&&[^*/]]\\*/"
,"/\\*.*&&(?!^*/$)\\*/"
,但即时通讯无法得到确切的解决方案。
特定字符串的字符串,我需要在字符串匹配/*exa*/mple*/
一个子正则表达式,正则表达式匹配不包含在Java中
匹配的字符串必须/*exa*/
不/*exa*/mple*/
。
它也不能包含其中的"*/"
。
我尝试了这些正则表达式:
"/\\*[.*&&[^*/]]\\*/"
,"/\\*.*&&(?!^*/$)\\*/"
,但即时通讯无法得到确切的解决方案。
我知道你想从文本中挑选评论。
Pattern p = Pattern.compile("/\\*.*?\\*/");
Matcher m = p.matcher("/*ex*a*/mple*/and/*more*/ther*/");
while (m.find()){
System.out.println(m.group());
}
你可以试试这个:
/\*[^\*\/\*]+\*/ --> anything that is in between (including) "/*" and "*/"
这里有一个例子:
Pattern p = Pattern.compile("/\\*[^\\*\\/\\*]+\\*/");
Matcher m = p.matcher("/*exa*/mple*/");
while (m.find()){
System.out.println(m.group());
}
OUTPUT:
/* * EXA/
试试这个[如何找到在Java中使用正则表达式的确切的词?(http://stackoverflow.com/questions/9464261/how-to-find-the-exact-word-using-a -regex-in-java) – Perdomoff
最简单的方法是使用两个正则表达式,匹配rexexp1 &&〜regexp2 –