54

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

10
  • Use $& instead of $0. Commented Oct 3, 2016 at 17:54
  • @WiktorStribiżew Just tested. Doesn't seem to work, though. Regrettably... Commented Oct 3, 2016 at 17:57
  • Replacing .+ with $& Donkey works, same as replacing with $0 Donkey Commented Oct 3, 2016 at 18:00
  • Seems * quantifier alone has some issues. But ^.*.$ is fine. Commented Oct 3, 2016 at 18:01
  • Seems like the bug is fixed on the newer versions of VS Code. I can repro the same in 1.2.0. Commented Oct 3, 2016 at 18:15

7 Answers 7

88

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

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

Sign up to request clarification or add additional context in comments.

13 Comments

I think you used ^(.+)$, right? The ^.*$ and ^(.*)$ do not work.
$0 Donkey and $1 Donkey both append \sDonkey to every line in my VSCode with @Andy J's Find Use ^(.+)$ if you want to append to lines with something in them only. VSC 1.5.3
Could you please confirm the version of your VSC? Mine is 1.2.0.
@WiktorStribiżew I used star, not plus. Just as quoted. The other two you mention do work on my environemnt. What version of VS are you using?
Mine is 1.2.0. Also, I cannot make ^$|^.+$ work, either. Strange as they work when I use them separately.
|
29

The easiest way to do it is like this:

Find: .$
Replace: $&Donkey

Comments

12

You can use:

$ = end of line ^ = begin of line

Comments

7

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

Comments

7

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.

4 Comments

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.
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).
@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.
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 mistaken 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.
6

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.