如何在字符串中搜索相同的模式,然后在Java中将它们带到数组中?

如何在字符串中搜索相同的模式,然后在Java中将它们带到数组中?,java,regex,arrays,string,Java,Regex,Arrays,String,比如说, 字符串是:“{aaa,bbb},{ccc,ddd},{eee,fff}” 我希望程序将其自动拆分为字符串模式 模式是:{{…},{…},{…} 什么是模式匹配正则表达式?已编辑。这里有一个更好的解决方案。您只需创建一次编译后的模式,然后通过“matcher()”例程针对每个输入字符串运行它 Matcher m=Pattern.compile(\\{(\\w*,\\w*)\}).Matcher({aaa,bbb},{ccc,ddd},{eee,fff}”); List stuffArra

比如说,

字符串是:“{aaa,bbb},{ccc,ddd},{eee,fff}”

我希望程序将其自动拆分为字符串模式

模式是:{{…},{…},{…}


什么是模式匹配正则表达式?

已编辑。这里有一个更好的解决方案。您只需创建一次编译后的模式,然后通过“matcher()”例程针对每个输入字符串运行它

Matcher m=Pattern.compile(\\{(\\w*,\\w*)\}).Matcher({aaa,bbb},{ccc,ddd},{eee,fff}”);
List stuffArray=new ArrayList();
for(int i=1;m.find();i++)
{
add(m.group().replaceAll(“[{}]”,“”);
}
String[]stuffString=stuffArray.toArray(新字符串[stuffArray.size()]);
String有一个
split(stringregex)
方法,可能会被证明很有用。它返回一个
字符串[]

你已经找到了模式

模式是:{{…},{…},{…}


这里有一个重复出现的主题,它限定了您试图提取的数组元素。您可能还想考虑如何处理模式中您不想要的起始位和结束位。

不确定您想要什么,因此:

备选案文1a 这将返回一个包含以下元素的
字符串[]

[ "aaa,bbb",
  "ccc,ddd",
  "eee,fff" ]
[ [ "aaa", "bbb" ],
  [ "ccc", "ddd" ],
  [ "eee", "fff" ] ]
如果使用原始字符串调用此项:

  public static String[] split1(String source) {
    final ArrayList<String> res = new ArrayList<String>();

    if (source != null) {
      source = source.trim();
      if (source.startsWith("{") && source.endsWith("}")) {
        final Pattern p = Pattern.compile("\\{([^}]+)\\}[,]?");
        final Matcher m = p.matcher(source.substring(1).substring(0, source.length() - 2));

        while (m.find()) {
          res.add(m.group(1));
        }
      }
    }
    return (res.toArray(new String[res.size()]));
  }
  public static String[][] split2(String source) {
    final ArrayList<String[]> res = new ArrayList<String[]>();

    if (source != null) {
      source = source.trim();
      if (source.startsWith("{") && source.endsWith("}")) {
        final Pattern p = Pattern.compile("\\{([^}]+)\\}[,]?");
        final Matcher m = p.matcher(source.substring(1).substring(0,
            source.length() - 2));

        while (m.find()) {
          res.add(m.group(1).split(","));
        }
      }
    }
    return (res.toArray(new String[res.size()][]));
  }
如果使用原始字符串调用此项:

  public static String[] split1(String source) {
    final ArrayList<String> res = new ArrayList<String>();

    if (source != null) {
      source = source.trim();
      if (source.startsWith("{") && source.endsWith("}")) {
        final Pattern p = Pattern.compile("\\{([^}]+)\\}[,]?");
        final Matcher m = p.matcher(source.substring(1).substring(0, source.length() - 2));

        while (m.find()) {
          res.add(m.group(1));
        }
      }
    }
    return (res.toArray(new String[res.size()]));
  }
  public static String[][] split2(String source) {
    final ArrayList<String[]> res = new ArrayList<String[]>();

    if (source != null) {
      source = source.trim();
      if (source.startsWith("{") && source.endsWith("}")) {
        final Pattern p = Pattern.compile("\\{([^}]+)\\}[,]?");
        final Matcher m = p.matcher(source.substring(1).substring(0,
            source.length() - 2));

        while (m.find()) {
          res.add(m.group(1).split(","));
        }
      }
    }
    return (res.toArray(new String[res.size()][]));
  }

不需要自动添加到阵列如何?我只需要模式匹配正则表达式,谢谢!谢谢我在哪里可以了解所有正则表达式符号的表示?既然常规语法无法处理嵌套,“稍微复杂一点”不是一种轻描淡写的说法吗?还有,我不确定我是否理解你的逻辑。删除所有逗号不会导致问题吗?javadoc on模式对于正则表达式非常全面。您可以在这里找到javadoc on模式:
public static String[][] split4(final String source) {
  final ArrayList<String[]> res = new ArrayList<String[]>();

  if (source != null) {
    final Pattern p = Pattern.compile("\\{(((\\w+),(\\w+))[,]?)+\\}");
    final Matcher m = p.matcher(source.trim());

    while (m.find()) {
      res.add(new String[] {
          m.group(3),
          m.group(4)
      });
    }
  }
  return (res.toArray(new String[res.size()][]));
}
public static void main(String[] args) {
  final String TEST = "{{aaa,bbb},{ccc,ddd},{eee,fff}}";

  System.out.println("split1 (Option 1a)");
  for (final String str : split1(TEST)) {
    System.out.println(str);
  }

  System.out.println("split2 (Option 2a)");
  for (final String[] strs : split2(TEST)) {
    System.out.println(Arrays.toString(strs));
  }

  System.out.println("split3 (Option 1b)");
  for (final String str : split3(TEST)) {
    System.out.println(str);
  }

  System.out.println("split4 (Option 2b)");
  for (final String[] strs : split4(TEST)) {
    System.out.println(Arrays.toString(strs));
  }
}