MSCF
MSCF

Reputation: 157

Notepad++ regex to find single character bounded by |

I'm having trouble coming up with the regex I need to do this find/replace in Notepad++. I'm fine with needing a couple of separate searches to complete the process.

Basically I need to add a | at the beginning and end of every line from a CSV, plus replace all the , with |. Then, on any value with only 1 character, I need to put two spaces around the character on each side ("A" becomes " A ")

Source:

col1,col2,col3,col4,col5,col6
name,desc,something,else,here,too
another,,three,,,
single,characters,here,a,b,c
last,line,here,,almost,

Results:

|col1|col2|col3|col4|col5|col6|
|name|desc|something|else|here|too|
|another||three||||
|single|characters|here|  a  |  b  |  c  |
|last|line|here||almost||

Adding the | to the beginning and the end of the line is simple enough, and replacing , with | is obviously straightforward. But I can't come up with the regex to find |x| where x is limited to a single character. I'm sure it is simple, but I'm new to regex.

Upvotes: 1

Views: 3247

Answers (3)

revo
revo

Reputation: 48711

Regex:

(?:(^)|(?!^)\G)(?:([^\r\n,]{2,})|([^\r\n,]))?(?:(,$)|(,)|($))

Replacement string:

(?{1}|)(?{2}\2)(?{3}  \3  )(?{4}||)(?{5}|)(?{6}|)

Ugly, dirty and long but works.

Regex Explanation:

(?:                 # Start of non-capturing group (a)
    (^)                 # Assert beginning of line (CP #1)
    |                   # Or
    (?!^)               # //
    \G                  # Match at previous matched position
)                   # End of non-capturing group (a)

(?:                 # Start of non-capturing group (b)
    ([^\r\n,]{2,})      # Match characters with more than 2-char length (any except \r, \n or `,`) (CP #2)
    |                   # Or
    ([^\r\n,])          # Match one-char string (CP #3)
)?                  # Optional - End of non-capturing group (b)

(?:                 # Start of non-capturing group (c) 
    (,$)                # Match `,$` (CP #4)
    |                   # Or
    (,)                 # Match single comma (CP #5)
    |                   # Or
    ($)                 # Assert end of line (CP #6)
)                   # End of non-capturing group (c) 

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The first replace adds | at the beginning and at the end, and replaces commas:

Search: ^|$|,
Replace: |

The second replace adds space around single character matches:

Search: (?<=[|])([^|])(?=[|])
Replace:   $1  

Add spaces to the left and to the right of $1.

Upvotes: 1

Sebastian Proske
Sebastian Proske

Reputation: 8413

Three Step Solution:

  • Pattern: ^.+$ Replacement: |$0|
  • Pattern: , Replacement: |
  • Pattern: (?<=\|)([^|\r\n])(?=\|) Replacement: $0

Upvotes: 1

Related Questions