Search code examples
regexnotepad++

notepad++ regex groups


i have the following lines in notepad++

'23123 123123
'23123 123123
'23123 123123
'23123 123123
'23123 123123

and i want to replace with

'23123' 123123
'23123' 123123
'23123' 123123
'23123' 123123

I am doing the following

Find What:     (\d)\s
Replace With: $0'

But it is not working


Solution

  • Your regex does not work because (\d)\s matches and captures a digit and then matches a any vertical or horizontal whitespace including line breaks. The replacement is the whole match and a ' char. Thus, you append ' to any digit + whitespace sequence that affects the digit chunks at the end of the second column.

    To add ' to the digit chunk at the start of the line you may use

    ^'\d+
    

    and replace with $0'.

    Details

    • ^ - start of the line anchor
    • ' - a single quote
    • \d+ - 1 or more digits.

    The replacement is the whole match value ($0) and a ' (basically, we append ' to each match).

    enter image description here

    An alternative approach is to insert ' in between a digit and a horizontal whitespace:

    (\d)(\h)
    

    and replace with $1'$2. It will append ' to all non-final digit chunks on a line:

    enter image description here

    Details

    • (\d) - Capturing group 1 (later referenced to with $1 placeholder): a digit
    • (\h) - Capturing group 2 (later referenced to with $2 placeholder): a horizontal whitespace.