0

For example,

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?

nhahtdh
56.9k15 gold badges131 silver badges164 bronze badges
asked Oct 15, 2010 at 15:24
1
  • How about no need auto add to array? I just need the Pattern Matching regex, thanks! Commented Oct 15, 2010 at 15:32

3 Answers 3

1

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));
 }
}
answered Oct 15, 2010 at 16:16
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Oct 15, 2010 at 15:38

Comments

0

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() ] );
answered Oct 15, 2010 at 15:36

4 Comments

Thanks! Where can I learn all the regex symbol is represent for?
Since regular grammars cannot handle nesting, isn't "a bit more complex" an understatement? Also, not sure I understand your logic. Isn't removing all the commas going to cause a problem?
The javadoc on Pattern is pretty comprehensive regarding regex expressions.
You can find the javadoc on Pattern here: download.oracle.com/javase/1.5.0/docs/api/java/util/regex/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.