【问题标题】:How to search same pattern in a string and then take them to array in Java?如何在字符串中搜索相同的模式,然后将它们带到 Java 中的数组中?
【发布时间】:2011-04-26 00:17:49
【问题描述】:

例如,

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

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

模式是:{{...},{...},{...}}

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

【问题讨论】:

  • 不需要自动添加到数组怎么样?我只需要模式匹配正则表达式,谢谢!

标签: java regex arrays string


【解决方案1】:

不确定你想要什么,所以这里是:

选项 1a

这将返回一个包含元素的String[]

[ "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()]));
  }

选项 1b

编辑:这比 1a 稍微简单一点,结果相同:

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

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

    while (m.find()) {
      res.add(m.group(2));
    }
  }
  return (res.toArray(new String[res.size()]));
}

选项 2a

这将返回一个包含元素的String[][]

[ [ "aaa", "bbb" ],
  [ "ccc", "ddd" ],
  [ "eee", "fff" ] ]

如果您使用原始字符串调用它:

  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()][]));
  }

选项 2b

编辑:这比 2a 稍微简单一点,结果相同:

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));
  }
}

【讨论】:

    【解决方案2】:

    String 有一个split(String regex) 方法,它可能被证明是有用的。它返回一个String[]

    你已经弄清楚了模式;

    模式为:{{...},{...},{...}}

    那里有一个反复出现的主题,用于界定您尝试提取的数组元素。您可能还想考虑如何处理您不想要的模式中的开始和结束位。

    【讨论】:

      【解决方案3】:

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

          Matcher m= Pattern.compile( "\\{(\\w*,\\w*)\\}" ).matcher( "{{aaa,bbb},{ccc,ddd},{eee,fff}}" );
          List<String> stuffArray = new ArrayList<String>();
          for ( int i = 1; m.find(); i++ )
          {
              stuffArray.add( m.group().replaceAll( "[{}]","" ) );
          }
          String[] stuffString = stuffArray.toArray( new String[ stuffArray.size() ] );
      

      【讨论】:

      • 谢谢!我在哪里可以了解所有正则表达式符号的代表?
      • 由于常规语法无法处理嵌套,“稍微复杂一点”不是轻描淡写吗?另外,不确定我是否理解您的逻辑。删除所有逗号不会导致问题吗?
      • Pattern 上的 javadoc 关于正则表达式非常全面。
      • 你可以在这里找到关于 Pattern 的 javadoc:download.oracle.com/javase/1.5.0/docs/api/java/util/regex/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 2016-02-21
      • 2015-03-10
      • 1970-01-01
      相关资源
      最近更新 更多