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

4th Feb 2018, 2:33 PM
Abradolf Lincler
Abradolf Lincler - avatar
3 ответов
+ 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
4th Feb 2018, 7:21 PM
David Carroll
David Carroll - avatar
+ 2
Thanks a lot, @David Carroll
5th Feb 2018, 12:29 AM
Abradolf Lincler
Abradolf Lincler - avatar
+ 1
The first capture group includes a "Look behind". This expression looks behind to find where a number of at least four digits starts.
4th Feb 2018, 5:15 PM
1of3
1of3 - avatar