Search code examples
regexnotepad++

Move comma-separated numbers and individual numbers to the next line, skipping specific lines


I have a list like following:

Play3
123,777
One
23,000
etet55
Hero12
vrtg1,345
2
Jan2003

Now I want to move comma-separated numbers and normal numbers to next line while skip specific lines.
lines that must skip:

^(Play3|One|Hero12)$
\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b  

values that must move to next lines:

(?<!\d)0*\K(?:\d{1,3}(?:,\d{3})+|(?!0+(?!\d))\d{1,4})(?!\d)  
\d+  

this mean above sample list must convert to following list:

Play3

123,777
One

23,000
etet
55
Hero12
vrtg
1,345

2
Jan2003

note that comma-separated numbers and normal numbers must placed at the end of line or line must start and end with them. lines can't start with a number and end with a character like following:

12Word

I tried following regex but I failed:

find : ^(Play3|One|Hero12)$|\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b|(?<!\d)0*\K(?:\d{1,3}(?:,\d{3})+|(?!0+(?!\d))\d{1,4})(?!\d)|\d+
replace : $1\n$2\n$3\n$4

find : ^(Play3|One|Hero12)$|\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b|\d+|0*\K(?:\d{1,3}(?:,\d{3})+|\d{1,4})
replace : $1\n$2\n$&

find : ^(Play3|One|Hero12)$|\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b|\d+(?:,\d{3})*|\d{1,3}(?:,\d{3})+
replace : $1\n$2\n$3

where is my problem?

note that (?<!\d)0*\K(?:\d{1,3}(?:,\d{3})+|(?!0+(?!\d))\d{1,4})(?!\d) regex can't select both of comma-separated and normal number values.


Solution

  • In notepad++ you can use 2 capture groups with a conditional replacement:

    Find what:

    ^(?:Play3|One|Hero12|.*\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b.*)$(*SKIP)(*F)|\d+(?:,\d+)*$
    
    • ^ Start of string
    • (?:Play3|One|Hero12|.*\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b.*)$ Match one of Play3 One Hero12 on a single line, or match a line that matches one of the Month names followed by 4 digits
    • (*SKIP)(*F) Skip the match
    • | Or
    • \d+(?:,\d+)* Match 1+ digits with an optional repeated decimal part in group 2
    • $ End of string

    Replace with:

    \n$0
    

    Regex demo


    enter image description here

    If you want to use the logic for the digits as well:

    ^(?:Play3|One|Hero12|\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\d{4}\b)$(*SKIP)(*F)|(?<![\d,])(?![0,]*$)(?:0*\K(?:\d{1,3}(?:,\d{3})+|\d{1,4}))$
    

    Regex demo