39

In Visual Studio Code I'm using the replacement feature. The regex option is activated and the parameters are as follows.

Find: ^.*$
Replace: \0 Donkey

While the editor seems to recognize the patters (judging by the highlight), there's no substitution being made. It works using other patterns but not the one including the line end.

I've also tried combinations including but not limited to the following.

Find: ^(.*)$
Replace: $0 Donkey

How can I do that?

A programmer had a problem once.
He said: "let's use regular expressions".
Then he had two problems...

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

7 Answers7

72

This patter does work on my VS Code but only on lines that actually contain something. Empty lines stay empty.

Find: ^(.*)$
Replace: $0 Donkey

28

The easiest way to do it is like this:

Find: .$
Replace: $&Donkey

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
10

You can use:

$ = end of line ^ = begin of line

Braytiner
  • 470
  • 4
  • 12
6

To add text at the end of each line and also at the beginning, in Visual Studio

find (with regex option activated)

^(.+)$

replace

Alpha $1 Zeta
Result

Before

some text

After

Alpha some text Zeta

Image screenshot result

Image screenshot result

Ax_
  • 803
  • 8
  • 11
1

To add quotes to the start of every line:

enter image description here

To add quotes to the end of every line:

enter image description here

1

Add single quotes in beginning and end with comma.

Find: (.*)$

Replace: '$1',

I want this:

 273794103
 418892296
 134582886

to be:

 '273794103',
 '418892296',
 '134582886',

I needed this for writing SQL query, instead of typing comma manually for 50 Ids :).

Reference: This and AI.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • 1
    I don't see how that addresses the actual question being asked. The point was to **include** the line-break as a part of the matched string and then to **render** it as a part of the produced output. – Konrad Viltersten Jul 23 '23 at 15:19
  • 1
    Also, I wonder about the "reference: AI" part. Is this a GTP-based answer? If so, I need to point out that it's not allowed on this site (and for good reasons, as it rarely produces a reliable and qualitative answer). Or did you find another source of AI-knowledge? If so, please share the link. I'd love to AI up my code (in a reliable way, of course). – Konrad Viltersten Jul 23 '23 at 15:21
  • @KonradViltersten , Although the accepted is correct , But I just answered this , i thought others may benefit from for similar issue for different type of output. . Also reference was mix of Human and Answer from Chat GPT. I have carefully checked what AI has generated and tested it and gave it as answer. – Shaiju T Jul 28 '23 at 09:02
  • 1
    The thought of broading the scope for others' use is exhilarating, so I'll even +1 you on this. However, be careful with those AI aided answers. You (apparently) know to skeptically review what the stupid GPT poops out. However, it's a sensitive and thin border - you don't want to be **mis**taken for one of those dilettantes who thoughtlessly drop junk into the black box entirely ignorant of the response's quality but moronically trusting that the produced result is useful. – Konrad Viltersten Jul 28 '23 at 17:09
0

as i came here with fraction of the question search,
have given this answer for new bee;
this will also work:

  1. press "ctrl+H" (find and replace);
  2. type "\n" (without quotes) and,
  3. beside find box, click and turn on, "use regular expression", as shown in picture below;
  4. now, in replace box, type your text (e.g. Donkey) and don't forget to add the same "\n" (without quotes) at the end. enter image description here
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42