regexnotepad++

Replace Spaces with New Lines in Comma-Separated Number Lines


I have a list like following:

\u042f 12,197,051
\ube44\ud574 93,721,409
HUMAN 45,754,677
Overwatch 43,437,031 OVERWATE

Now I want to replace spaces with new lines in Comma-Separated number lines like following results:

\u042f
12,197,051
\ube44\ud574
93,721,409
HUMAN
45,754,677
Overwatch
43,437,031
OVERWATE

I tried following regex:

Find What: (^\h*)(.*?)(\h*,.*$)
Replace With: \1\2\n\3

But it not working good and provide me following results:

\u042f 12
,197,051
\ube44\ud574 93
,721,409
HUMAN 45
,754,677
Overwatch 43
,437,031 OVERWATE

Where is my regex problem?


Solution

  • If you want to replace leading spaces before comma separated numbers:

    \h+(\d+(?:,\d+)+)(\h+)?
    

    The pattern matches:

    See a regex demo

    In the replacement a newline followed by the group 1 value, and if there is a group 2 value append another newline using a conditional:

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

    enter image description here