开发者

How to search same pattern in a string and then take them to array in Java?

开发者 https://www.devze.com 2023-01-20 07:19 出处:网络
For e开发者_JAVA技巧xample, The string is: \"{{aaa,bbb},{ccc,ddd},{eee,fff}}\" I want the program auto split it as a string pattern

For e开发者_JAVA技巧xample,

The string is: "{{aaa,bbb},{ccc,ddd},{eee,fff}}"

I want the program auto split it as a string pattern

Pattern is: {{...},{...},{...}}

What is the Pattern Matching regex?


Not sure of what you want, so here goes:

Option 1a

This will return a String[] containing elements:

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

if you call this with your original string:

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

Option 1b

EDIT: this is slightly simpler than 1a, for the same result:

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

Option 2a

This will return a String[][] containing elements:

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

if you call this with your original string:

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

Option 2b

EDIT: this is slightly simpler than 2a, for the same result:

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

Here's a main method for testing:

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


String has a split(String regex) method that might prove useful. It returns a String[].

You've figured out the pattern;

Pattern is: {{...},{...},{...}}

there is a recurring theme there that delimits the array elements you are trying to extract. You might also want to think about how you handle the start and end bits in your pattern that you don't want.


Edited. Here's a better solution. You'd just create the compiled Pattern once, then run it against each input string via the "matcher()" routine.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号