Do you haven any useful example of the boundary matcher "\G"`? Please give me some real world examples. Java source is appreciated. From "Mastering regular expressions. Jeffrey E. F. Friedl" I got an useful example parsing HTML but I am not sure how if a translation to Java is possible.
Asked
Active
Viewed 1.5k times
4
-
I addressed a similar question recently: http://stackoverflow.com/a/14366062/211627 – JDB Jan 16 '13 at 20:38
2 Answers
6
This is a regex-based solution to introduce thousand separators:
String separateThousands(String s) {
return s.replaceAll(
"(?<=\\G\\d{3})(?=\\d)" + "|" + "(?<=^-?\\d{1,3})(?=(?:\\d{3})+(?!\\d))",
","
);
}
This will transform "-1234567890.1234567890"
to "-1,234,567,890.1234567890"
.
See also
- codingBat separateThousands using regex (and unit testing how-to)
- Explanation of how it works, and alternative regex that also uses
\G
.
- Explanation of how it works, and alternative regex that also uses
This one is more abstract, but you can use \G
and fixed-length lookbehind to split
a long string into fixed-width chunks:
String longline = "abcdefghijklmnopqrstuvwxyz";
for (String line : longline.split("(?<=\\G.{6})")) {
System.out.println(line);
}
/* prints:
abcdef
ghijkl
mnopqr
stuvwx
yz
*/
You don't need regex for this, but I'm sure there are "real life" scenarios of something that is a variation of this technique.

Community
- 1
- 1

polygenelubricants
- 376,812
- 128
- 561
- 623
-
1That's very nice. I hope other authors will bring some more real world examples to the table. – Mister M. Bean Apr 26 '10 at 07:12
-
I don't see the point of `"(?:%s)|(?:%s)"`? That should just be `"%s|%s"`. – Christoffer Hammarström Apr 28 '10 at 13:32
-
@Christoffer: I was just playing it ultra-safe for the general case where "left" or "right" can have its own top-level alternation, but I think you're right: even in that case, it'll still work. – polygenelubricants Apr 28 '10 at 14:19
-1
Here's an example:
Pattern p = Pattern.compile("\\Gfoo");
Matcher m = p.matcher("foo foo");
String trueFalse = m.find() + ", " + m.find();
System.out.println(trueFalse);
Pattern p1 = Pattern.compile("foo");
Matcher m1 = p1.matcher("foo foo");
String trueTrue = m1.find() + ", " + m1.find();
System.out.println(trueTrue);

JRL
- 76,767
- 18
- 98
- 146