tt0686
tt0686

Reputation: 1849

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

Upvotes: 3

Views: 4840

Answers (4)

Haji Rahmatullah
Haji Rahmatullah

Reputation: 430

Try my solution too is as below

Search: '\d+

Replace: $0'

enter image description here

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 177674

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.

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163362

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

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626853

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.

Upvotes: 2

Related Questions