Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(?<!\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)|([^\s\d])?(\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
  • ([^\s\d])? Optionally match a non whitespace char other than a digit in group 1
  • (\d+(?:,\d+)?) Match 1+ digits with an optional decimal part in group 2
  • $ End of string

Replace with:

(?{1}$1\n$2:\n$2)

The replacement tests for group 1. If it exists, then replace with $1\n$2 else replace with \n$2

Regex demo


enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading