i am pretty ok with basic reg-ex. but this line of code used to make the thousand separation in large numbers exceeds my knowledge and googling it quite a bit did also not satisfy my curiosity. can one of u please take a minute to explain to me the following line of code?
someString.replaceAll("(\\G-?\\d{1,3})(?=(?:\\d{3})++(?!\\d))", "$1,");
i especially don’t understand the regex structure "(?=(?:\d{3})++(?!\d))".
thanks a lot in advance.
"(?=(?:\d{3})++(?!\d))"is a lookahead assertion.It means “only match if followed by ((three digits that we don’t need to capture) repeated one or more times (and again repeated one or more times) (not followed by a digit))”. See this explanation about
(?:...)notation. It’s called non-capturing group and means you don’t need to reference this group after the match."(\\G-?\\d{1,3})"is the part that should actually match (but only if the above-described conditions are met).Edit: I think
+must be a special character, otherwise it’s just a plus. If it’s a special character (and quick search suggests that it is in Java, too), the second one is redundant.Edit 2: Thanks to Alan Moore, it’s now clear. The second
+means possessive matching, so it means that if after checking as many 3-digit groups as possible it won’t find that they’re not followed by a non-digit, the engine will immediately give up instead of stepping one 3-digit group back.