Reputation: 153
In Java, I will be parsing scripts and extracting any text from the file that begins with
${GLOBAL_} or ${AUTO_}
I want to pickup everything until the last curly brace, so for example if I had the following String:
"this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2} ${OTHER_VAR}"
The results should be:
${AUTO_TEST_f}
${GLOBAL_SYNC}
${AUTO_2}
I've attempted to create regex patterns (that I believe do work) and using them to create a Matcher. I then attempt to use the Matcher to print all the matches to console but I'm having some issues. For some reason, it's skipping over ${GLOBAL_VARIABLE_1}. Also, how could I implement this to give me all the matches? A while loop [while match.group(0) != null]?
Here's my code:
String re1="(\\$)"; // Any Single Character 1
String re2="(\\{)"; // Any Single Character 2
String re3="(G)"; // Any Single Character 3
String re4="(L)"; // Any Single Character 4
String re5="(O)"; // Any Single Character 5
String re6="(B)"; // Any Single Character 6
String re7="(A)"; // Any Single Character 7
String re8="(L)"; // Any Single Character 8
String re9="(_)"; // Any Single Character 9
String re10="(.*?)"; // Any Single Character 10
String re11="(\\})"; // Any Single Character 11
String r1="(\\$)"; // Any Single Character 1
String r2="(\\{)"; // Any Single Character 2
String r3="(A)"; // Any Single Character 3
String r4="(U)"; // Any Single Character 4
String r5="(T)"; // Any Single Character 5
String r6="(O)"; // Any Single Character 6
String r7="(_)"; // Any Single Character 7
String r8="(.*?)"; // Any Single Character 8
String r9="(\\})"; // Any Single Character 9
Pattern p = Pattern.compile((re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11));
Pattern p2 = Pattern.compile(r1+r2+r3+r4+r5+r6+r7+r8+r9);
Matcher m = p.matcher(txt);
Matcher m1 = p2.matcher(txt);
m1.find();
System.out.println(m1.group(0));
m.find();
System.out.println(m.group(0));
And this is the Console Results:
Actual Results:
${AUTO_1}
${GLOBAL_VARIABLE_2}
Here are my expected Results:
Expected Results:
${GLOBAL_VARIABLE_1}
${AUTO_1}
${GLOBAL_VARIABLE_2}
${GLOBAL_VARIABLE_3}
Thanks!
Upvotes: 0
Views: 5593
Reputation: 37813
Don't overcomplicate:
String txt = "this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2}";
String regex = "\\$\\{(AUTO|GLOBAL)_(.*?)\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(txt);
while (matcher.find()) {
System.out.println(matcher.group() + "\t->\t" + matcher.group(2) + "\t(" + matcher.group(1) + ")" );
}
Output:
${AUTO_TEST_f} -> TEST_f (AUTO)
${GLOBAL_SYNC} -> SYNC (GLOBAL)
${AUTO_2} -> 2 (AUTO)
Upvotes: 5
Reputation: 56809
Whatever you are doing, it is not the right way to code. Composing a regex from smaller components is fine, but it is pointless when you break the component down to single character.
If you want to get those that starts with GLOBAL
or AUTO
, it is as simple as:
\$\{(GLOBAL|AUTO)_.*?\}
Putting the regex into a string literal:
"\\$\\{(GLOBAL|AUTO)_.*?\\}"
Upvotes: 1
Reputation: 7804
Try this out :
String data = "this is a String ${AUTO_TEST_f} body ${GLOBAL_SYNC} ${AUTO_2}";
Pattern pattern = Pattern.compile("\\$\\{.+?\\}");
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
// Indicates match is found. Do further processing
System.out.println(matcher.group());
}
Output is :
${AUTO_TEST_f}
${GLOBAL_SYNC}
${AUTO_2}
Upvotes: 1