3

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

3
  • Please check both the solutions in my answer, please let me know which one suits your scenario best. Commented Jul 4, 2018 at 8:23
  • Describing how it is not working would help answers understand your issue. Commented Jul 4, 2018 at 8:55
  • Glad my answer worked for you. Please also consider upvoting all answers that proved helpful to you. Commented Jul 4, 2018 at 9:17

4 Answers 4

2

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.
0

Your regex would work if you turned it all around to \s(\d) and replace with '$0

Note that in that case you don't need the capturing group and \s\d would also work.

If there have to be one or more digits, another way for your example data could be to match one or more horizontal whitespaces \h+ followed by one or more digits \d+ and to replace with a ' and the whole match $0

Find what

\h+\d+

Replace with

'$0

0

As an alternative to a regular expression in this case, Notepad++ allows column selection. Click on the column in the first line and press ALT-SHIFT-DOWN ARROW until the bottom row is reached. The cursor will extend:

Image of extended cursor

Then type ' and it will be typed in all rows.

5
  • 1
    FYI: This solution will only work if the number of digits in the first column is constant. It is also "manual", you cannot use it to process multiple files at once, nor does it help automate processing more than one column values "at once". Commented Jul 4, 2018 at 9:03
  • @WiktorStribiżew Let me stress alternative and in this case. Commented Jul 4, 2018 at 9:07
  • I understand, I just think OP wouldn't have gone the regex way if the problem was that easy. Commented Jul 4, 2018 at 9:09
  • @WiktorStribiżew These answers are not just for the OP. Column selection is useful and not always well known. You already provided an answer. This was just an alternative if relevant. Commented Jul 4, 2018 at 9:10
  • Mark, I can't agree more. That is why I added my top comment, so that all future visitors could know of the solution limitations. It was not a comment for you. Commented Jul 4, 2018 at 10:41
0

Try my solution too is as below

Search: '\d+

Replace: $0'

enter image description here

1
  • 1
    How is it different from this answer above? Moreover answer with no explanation is completly useless.
    – Toto
    Commented Feb 20, 2021 at 9:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.