Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match the start of a line using a Visual Studio Code regex?

I'm trying to perform a simple operation: take a file and put "> " at the front of every line. However, when I try to use Visual Studio Code to do it, the regular expression "^" doesn't match all the lines. In particular, it matches:

  • blank lines
  • lines starting with "-", "{" or " " but not
  • lines starting with a letter

This makes no sense to me, I'm told it uses Rust's regular expression engine but I can't see anything in the documentation that would suggest this would happen.

Why does this happen and how do I fix it?

well this isn't ideal

This is what happens if I try "^.".

^.

like image 817
Julian Birch Avatar asked Mar 14 '18 21:03

Julian Birch


1 Answers

The Visual Studio text editor has a Regex implementation. You could populate this with some of your data and develop your Regex expression manually before you code it. I'm looking at Visual Studio Code (an MS product) on Linux and using the equivalent of

Search ^(.*)$ Replace >$1

in the editor I may have solved your problem.

-999 
{42

 uuu
AAA

becomes

>-999
>{42
>
> uuu
>AAA

This Regex technique is called group capturing.

like image 138
Aethelbald Avatar answered Sep 21 '22 03:09

Aethelbald