2015-10-09 53 views
1
String name="[COLOR dxvx]yyyy[/COLOR]" 
String name="[COLOR dvvx]tttt[/COLOR]" 

我需要得到的字符串YYYY是[COLOR dxvx]和之间创建正则表达式匹配[/ COLOR]怎么能做到这一点使用正则表达式中的Java如何4个分隔符

中指出[COLOR dxvx]旁边的颜色字也在变化

+0

你到目前为止做了什么? – MBaas

回答

0

您也可以尝试直接匹配:

(?<=])[^]]+(?=\\[/COLOR) 

DEMO

实现在Java中:

public static void main(String[] args){ 
    String[] strings = {"String name=\"[COLOR dxvx]yyyy[/COLOR]\"", 
      "String name=\"[COLOR dvvx]tttt[/COLOR]\""}; 

    for(String string : strings) { 
     ArrayList<Integer> numbers = new ArrayList<Integer>(); 
     Matcher matcher = Pattern.compile("(?<=])[^]]+(?=\\[/COLOR)").matcher(string); 
     if(matcher.find()){ 
      System.out.println(matcher.group()); 
     } 

    } 
} 

与输出:

yyyy 
tttt 
3

使用捕获组。

Matcher m = Pattern.compile("\\[COLOR\\s+dxvx\\](.*?)\\[/COLOR\\]").matcher(s); 
while(m.find()) { 
    System.out.println(m.group(1)); 
} 
+0

thanx为您的答复。在[COLOR dxvx]旁边,“COLOR”世界旁边没有定义。那也正在改变.. –

+0

尝试'“\\ [COLOR \\ s + \\ w + \\](。*?)\\ [/ COLOR \\]”' –