-1

我有一个字符串,我需要从中删除两个特定字符/单词之间的空格,这是示例:

String Test = " testcase s(this_one_has_no_space) and s(this_one_has_ space )"

我想删除后面每个's('和')'之间的空格。我怎样才能做到这一点?

4

2 回答 2

0
        String test = " testcase s(this_one_has_no_space) and s(this_one_has_ space )";
        Pattern p = Pattern.compile("s\\([^\\)]*\\s+[^\\)]*\\)");
        Matcher m = p.matcher(test);
        while (m.find()) {
            String temp = m.group();
            test = test.replace(temp, temp.replaceAll("\\s", ""));
        }
        System.out.println(test);

试试这个,看看你的问题能不能解决。

于 2013-09-20T19:56:45.023 回答
0

然后我会尝试捕获内括号,替换该字符串的空格,然后将
字符串放回原处。如果Java可以进行回调,那就可以了..

 //  s\\(([^()\\t\\ ]*[\\t\\ ][^()]*)\\)     

 s                                           
 \(                                         
 (                          # (1 start)      
      [^()\t\ ]*                           
      [\t\ ]                               
      [^()]*                                 
 )                          # (1 end)        
 \)                                         
于 2013-09-20T20:03:07.570 回答