Search code examples
regexnotepad++

Keep only numbers with comma separator in numerical lines


I have some values like following:

630,465
theGlobe
504,685
OpenDiary
Open
Diary
17,738
JUNE
1998
/////////////////////////
Bolt
bolt3,270,300
Classmates
1,509,260

Now i want to remove extra content from numerical lines.
for example I want to remove bolt from bolt3,270,300

I tried following regex:
Find What:
^.?(\b\d{1,3}(?:,\d{3})\b).*
Replace With:
$1

but this regex remove bolt3 from bolt3,270,300

I tried following regex too:
Find What:
^.?(\d{1,3}(,\d{3})).*
Replace With:
$1

but this regex remove 8 from 1998

How to fix this?


Solution

  • You can use

    ^.*?(?<!\d)(?<!\d,)(\d{1,3}(?:,\d{3})*)(?!,?\d).*
    

    See the regex demo.

    Details:

    • ^ - start of a line
    • .*? - any zero or more chars other than line break chars as few as possible
    • (?<!\d) - no digit allowed immediately on the left
    • (?<!\d,) - no digit and comma allowed immediately on the left
    • (\d{1,3}(?:,\d{3})*) - Group 1 ($1): one, two or three digits and the zero or more occurrences of a comma + three digits
    • (?!,?\d) - no digit or comma + digit allowed immediately on the right
    • .* - any zero or more chars other than line break chars as many as possible.