Search code examples
regexnotepad++

Enclose string within braces


I have a String of which comes like

POC41
POC85
POC71
POC03

and I want this to be changed to ('POC41','POC85','POC71','POC03') For this I use Notepad++ and do find \r\n and replace as ',' and select Extended option and replace all. Then I join the string with npp function and get the outcome as POC41','POC85','POC71','POC03','

I want the outcome to come as ('POC41','POC85','POC71','POC03')

Is there any regular expression in notepad++ by which I can achieve like this?


Solution

  • This can't be done in a single step.

    step 1

    • Ctrl+H
    • Find what: (.+)(\R)?
    • Replace with: '$1'(?2,)
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    (.+)        # group 1, 1 or more any character but newline
    (\R)?       # group 2, any kind of linebreak, optional
    

    Replacement:

    '$1'        # content of group 1 surrounded by single quotes
    (?2,)       # condiotnal replacement, if group 2 exists, add a comma
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here

    step 2

    • Find what: ^.+$
    • Replace with: \($0\)
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all