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?
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?
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() ] );
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.
Not sure of what you want, so here goes:
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()]));
}
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()]));
}
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()][]));
}
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));
}
}