H
Hal Vaughan
I can't find an actual reference to this in the API (I'm using 1.4.2), so I
want to be sure what I'm doing is "legal."
I'm using regexes to find any place in a string where there is a small
letter followed by a capital one and put a space between them. I'm
using "([a-z])([A-Z])" as the pattern to search for and using "$1 $2" as
the replacement string. This is working well in all my tests, but since I
didn't find it documented where I'd feel safe, I thought I should check
(I've also learned, in Perl, just how tricky regexes can be).
Is it correct that $1 in a replacement string references the first captured
text sequence in the regex? And so on with $2, $3....?
I've included my test case below (and I've tested more strings than what I
have in the code for now). I just want to be sure there aren't side
effects or other issues I'm not aware of!
Thanks!
Hal
---------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
public static void main(String[] args) {
Tester tTest = new Tester();
tTest.test("1AlphaBeta");
tTest.test("AlphaBetaGammaDelta");
for (int x = 0; x < args.length; x++) {
tTest.test(args[x]);
}
}
public Tester() {}
public void test(String sInput) {
String sPattern = "([a-z])([A-Z])", sOutput = "", sReplace = "$1 $2";
Pattern pRegex;
Matcher mRegex;
pRegex = Pattern.compile(sPattern);
mRegex = pRegex.matcher(sInput);
sOutput = mRegex.replaceAll(sReplace);
System.out.println("Input: " + sInput + ", Output: " + sOutput);
return;
}
}
want to be sure what I'm doing is "legal."
I'm using regexes to find any place in a string where there is a small
letter followed by a capital one and put a space between them. I'm
using "([a-z])([A-Z])" as the pattern to search for and using "$1 $2" as
the replacement string. This is working well in all my tests, but since I
didn't find it documented where I'd feel safe, I thought I should check
(I've also learned, in Perl, just how tricky regexes can be).
Is it correct that $1 in a replacement string references the first captured
text sequence in the regex? And so on with $2, $3....?
I've included my test case below (and I've tested more strings than what I
have in the code for now). I just want to be sure there aren't side
effects or other issues I'm not aware of!
Thanks!
Hal
---------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
public static void main(String[] args) {
Tester tTest = new Tester();
tTest.test("1AlphaBeta");
tTest.test("AlphaBetaGammaDelta");
for (int x = 0; x < args.length; x++) {
tTest.test(args[x]);
}
}
public Tester() {}
public void test(String sInput) {
String sPattern = "([a-z])([A-Z])", sOutput = "", sReplace = "$1 $2";
Pattern pRegex;
Matcher mRegex;
pRegex = Pattern.compile(sPattern);
mRegex = pRegex.matcher(sInput);
sOutput = mRegex.replaceAll(sReplace);
System.out.println("Input: " + sInput + ", Output: " + sOutput);
return;
}
}