在String中返回相同的子字符串

时间:2017-01-16 11:39:55

标签: java

这里我有以下代码。我的想法是使用像"R#GUPW*UIOPW#WERTY*RT#LLOPPPPER*CVW"这样的短语来返回类似

的模式

"#GUPW*"
"#WERTY*"
"#LLOPPPPER*"

返回以#开头并以*结尾的短语中的所有子串。所以在这个短语中有3个。

import java.io.*;

public class Pattern {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String phrase = "R#GUPW*UIOPW#WERTY*RT#LLOPPPPER*CVW";
        String found ="";

        for (int y = 0; y<phrase.length(); y++)
        {
        found = found + phrase.substring(phrase.indexOf("#"), phrase.indexOf("*"));
        }
        System.out.println(found);
    }

}

但是我的代码返回了 "#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW#GUPW"

如何返回这些以#开头并以我的短语结尾的子串。

"#GUPW*"
"#WERTY*"
"#LLOPPPPER*"

5 个答案:

答案 0 :(得分:2)

解决方案是定义Pattern,然后在Matcher中使用它。

List<String> allMatches = new ArrayList<String>();
Pattern p = Pattern.compile("#[^*]+\\*");
Matcher m = p.matcher(yourString);
while (m.find()) {
   allMatches.add(m.group());
 }

然后,所有匹配都会显示在List

演示:

String s = "R#GUPW*UIOPW#WERTY*RT#LLOPPPPER*CVW";
List<String> allMatches = new ArrayList<String>();
Pattern p = Pattern.compile("#[^*]+\\*");
Matcher m = p.matcher(s);
while (m.find()) {
    allMatches.add(m.group());
}

for (String str : allMatches) {
    System.out.println(str);
}
>>#GUPW*
>>#WERTY*
>>#LLOPPPPER*

答案 1 :(得分:1)

我只是搜索每个*,然后从这个位置找到之前的#(如果有的话)。这看起来像这样:

String input = "R#test*foo#bar#far#bor*for#";

int index = -1, begin;
while((index = input.indexOf("*", index+1)) > -1){
     if((begin = input.lastIndexOf("#", index)) > -1){ //If there is a character
          // Substract it from the current String
          System.out.println(input.substring(begin, index + 1));
      }
}

基本上,使用

*开始搜索下一个index + 1(+ 1以防止无限循环)
input.indexOf("*", index+1)) 

从那里开始,只需使用String.lastIndexOf从此位置搜索上一个#。如果Substring中有两个*,则此解决方案将同时打印两个。

此输出:

#test
#bor

答案 2 :(得分:0)

Apache Commons Libraries中的String utils中已有一种方法可以帮助您完成此任务。方法是StringUtils。substringsBetween

  

public static String [] substringsBetween(String str,                                            字符串打开,                                            String close)在String中搜索由开始和结束标记分隔的子字符串,返回all   匹配数组中的子串。

     

null输入String返回null。 null open / close返回null(no   比赛)。空(“”)打开/关闭返回null(不匹配)。

StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"] 
StringUtils.substringsBetween(null, *, *)            = null 
StringUtils.substringsBetween(*, null, *)            = null 
StringUtils.substringsBetween(*, *, null)            = null 
StringUtils.substringsBetween("", "[", "]")          = []  
  

参数:

     
      
  • str - 包含子串的String,null返回null,empty返回空
  •   
  • open - 标识子字符串开头的字符串,空返回null close - 标识
  • 的字符串   
  • end - of substring,empty返回null返回:一个字符串的子串数组,如果不匹配则返回null
  •   

此方法不包括输出中的分隔符,因此如果您确实需要它,我猜您需要用几行代码修复行为。

答案 3 :(得分:0)

尝试将循环内的代码更改为

int stIndex = phrase.indexOf("#");
int endIndex = phrase.indexOf("*");
found = found + "\n" + phrase.substring(stIndex, endIndex);
phrase = phrase.substring(endIndex + 1);

我做的是找到第一个匹配后我从输入中删除匹配的字符串。

答案 4 :(得分:-2)

   String phrase = "R#GUPW*UIOPW#WERTY*RT#LLOPPPPER*CVW";
   String[] ph = phrase.substring(phrase.indexOf("#"), phrase.lastIndexOf("*")+1) //get everything between the first "#" and last "*"                           
                       .replaceAll("\\*.*?#", "*#") // remove all chars between * and #
                       .split("(?<=\\*)"); //split at * and keep the delimiter
   System.out.println(Arrays.toString(ph));
相关问题