+ 2
New to programming,and I’m having trouble understanding regex?
I found this while studying regular expressions: If you want to Add a comma to every three digits of a very long number (of course from the right), you can look for sections that need to be preceded and added with commas like this: ((?<=\d)\d{3})+\b How does this expression works exactly?
3 Respostas
+ 4
Expanding on what @1of3 correctly explained, I'll try to break each part of this down for you:
First, let's focus only on everything inside the outermost parenthesis:
(?<=\d)\d{3}
-----------
(?<=\d) -> Positive look behind matches 1 numeric digit...
\d{3} -> Followed immediately by 3 consecutive numeric digits.
-----------
- NOTE: While look behind expressions are used to qualify a match, their matches are NOT included in the capture groups.
Next, let's focus on everything outside of the outermost parenthesis.
( ... )+\b
-----------
( ... ) -> The only capture group in this expression where each group contains exactly 3 numeric digits.
+ -> Match must include at least 1 or more capture groups...
\b -> Where the last character in the string is a word boundary character (ie. space, line break, punctuation, non word character)
A better way to understand how this works is to review this on an expression with tests I created on Regex101:
- https://regex101.com/r/XXqD5L/2
+ 2
Thanks a lot, @David Carroll
+ 1
The first capture group includes a "Look behind".
This expression looks behind to find where a number of at least four digits starts.