4

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.

Mister M. Bean
  • 501
  • 2
  • 5
  • 6
  • I addressed a similar question recently: http://stackoverflow.com/a/14366062/211627 – JDB Jan 16 '13 at 20:38

2 Answers2

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


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
-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