Find Tab at beginning of string and replace - regex

I am using this to find ^\t+ a tab at the beginning of the string and replace it with a space, the issue is that if the string has more than one tab it wont replace it with multiple spaces. how can i replace the tab on the beginning with the same amount of spaces?

You may use
\G\t
See the regex demo
The \G matches the start of string and the end of the previous successful match and \t will match 1 tab. With multiple search mode enabled (global mode), you will replace each tab at the start of the string with a space.
If you deal with tabs at the beginning of a line, you may use
(?:^|\G)\t
This expression was tested and works well in Notepad++.

Related

Match all spaces after a particular string

Doing a find and replace in VsCode on a large amount of files. I'm looking to replace all spaces after a set of quotes, but only on a specific line.
I can very easily find all spaces using \s+, but I don't understand how to capture only the spaces after a specific string(one specific line). I've tried positive look behinds, but I can only get it to match the first space, but I need to match all spaces on that line.
Example code:
variable = "01 - Testing this thing"
I need to find and replace all the spaces between the quotation marks with underscores, but I can't get any regex to match all the spaces between the quotes. I might want to replace the dash(-) as well, but the spaces are more important and I'm struggling to figure it out.
Here is a pretty good workflow.
Open a Search Editor (from the Command Palette or set a keybinding to it).
Use this regex (?<=variable = ")[^"]*.
That will find all matches in all files in your workspace or whatever folders you designate in the file to include filter. I suggest setting the context lines option to 0.
Ctrl+Shift+L to select all your matches. The matches are the 01 - Testing this thing part.
Now do a regular find in that search editor tab - with the Find in Selection option enabled.
Simply doing a find of and replaceAll with _ will make all those changes (in the Search Editor only).
To apply those changes to all the files with your initial search results, use the extension search-editor-apply-changes Apply Search Editor Changes... command.
Then you can check to see if the changes were as you expected and save all. It will open all affected files so you can inspect them.
Seems like a few steps but notice the first regex can be very simple. And then you are doing a simple find/replace in just those selections. Demo:
You search for a string that matches, it has A space between the quotes. Replace with what is before and after the space but the space is now an underscore. You have to apply this as often as the max number od spaces in a string. It can't be done in 1 regex search-replace.
In the Search Bar
Find Regex:
(variable = "[^" ]*) ([^"]*")
Replace:
$1_$2
Then apply Replace All (button) and Refresh (button) until no more searches found.

How to use backreference to replace part of the string?

.*\t.*\t.*\t.*
I have a 4-column table with 3 tabs as above. How can I replace the 2nd and 3rd tabs as comma in vim? I was trying to use vim to do that, but failed.
Here's one way to do it:
:%s/\(\t.\{-}\)\#<=\t/,/g
It uses a look-behind match to find a previously occurring tab character on the line, so it will match all tabs except for the first, so it will replace the 2nd, 3rd, 4th, etc. tab characters with commas. See :help /\#<= for help on the look-behind operator.
Another way, matching only the second and third tab of a line, and only lines with at least two tab characters, is to use a backreference \1 to store and refer to the contents in between the tabs.
:%s/\t.\{-\}\zs\t\(.\{-}\)\t/,\1,/
This also uses .\{-}, which matches 0 or more characters, but is non-greedy (so it tries to match the smallest sequence possible and stays close to the beginning of the line) and also the \zs marker to only start the replacement at that part of the match (just before the second tab of the line.) Again, see Vim's help docs on search patterns for more details on all those.

Regex to replace spaces with tabs at the start of the line

I'd like to be able to fix a Text File's tabs/spaces indentation.
Currently each line has spaces in random locations for some reason.
For example:
space tab if -> tab if
space tab space tab if -> tab tab if
tab tab space if -> tab tab if
etc.
It should not affect anything after the first word, so only the indentation will be affected: So tab space if space boolean should be changed to tab if space boolean not tab if tab boolean.
The regex command should keep the correct number of tabs and just remove the spaces.
If there are 4 spaces in a row it should be converted to a tab instead:
space space space space -> tab
Thank you for your help. If you could also explain how your regex works it would be very much appreciated as I'm trying to learn how to do my own regex instead of always asking others to do it.
If you need any more information or details please ask I'll respond as quickly as I can.
I can accomplish this for a single case at a time like so:
For spaces first: Find: space*if Replace: if This only works for lines with no tabs and where the first word is if so I would do this for the starting word of the line.
Then I would repeat with space*\tif.
Looks like I can match a word without capturing by doing (?:[A-Za-z]) So I can just swap out the if for this and it'll work better.
You could probably do this in one step, but I'm more partial to simple approaches.
Translate the 4 spaces to tabs first. First line is the match, second is the replace.
^(\s*)[ ]{4}(\s*)
$1\t$2
Then replace all remaining single spaces with nothing.
^(\t*)[ ]+
$1
You don't need the square brackets in this case, but it's a little hard to be sure that there's a space, even with SO's code formatting.
The first line searches for the start of the line ^, then finds any amount of whitespace (including tabs) and puts them in a matching group later named $1 with (\s*). The middle finds exactly four spaces [ ]{4}. The last part repeats the matching group in case there are tabs or more spaces on that side, too.
Since the second match is supposed to be finding all the remaining spaces, the second just looks for 0 or more tabs, puts them in a capture group, and then finds any remaining spaces left. Since it finds and replaces as it goes along, it gobbles up all spaces and replaces with tabs.

Regex to remove the first 2 lines of a text file

I am trying to delete only the first 2 lines of a text file.
I tried using \A.*, but this gets the first line and deletes the rest.
Is there a way to do the inverse?
It is maybe not the most convenient way, but it is possible with Regex:
^.*\n.*\n([\s\S]*)$
With default settings (neither single-line nor multi-line modifiers) the '.' captures everything, except newline. Therfore, .*\n captures one line, including the new line character. Repeat it twice, and we are at the beginning of the third line. Now capture all characters, including the new line character ([\s\S] is a nice workaround for this behavior) until the end of the file $.
Then substitute by the first capturing group
\1
and you have everything but the first 2 lines.
The details depend on your regex engine, how you give the substitute string. And depending on the platform or the used new line character of the file, you might need to exchange the \n with \r\n or \r or the one that matches it all (\r\n?|\n).
Here is a working Demo.

How to use Regex to move everything onto one line in notepad++

I'm trying to figure out how to use Regex to merge the contents of my text file
(25 lines of data) into one line.
So far, I can get Notepad++ to successfully find the lines I'm looking for by making it search for (^) , but what I'm unsure of is what is what to replace it with.
Syntax-wise I'm looking for the correct script that essentially attaches the beginning of one line to the end of the previous one. Can anyone help? Thanks
Find \R and replace with empty string.
\R matches multiple linebreak styles, including most common \r\n and \n.
Search Mode must be set to Regular expression.
Highlight the lines you want to join (or use Ctrl + A to select everything)
Choose Edit → Line Operations → Join Lines from the menu or press Ctrl + J.
It will put in spaces automatically if necessary to prevent words from getting stuck together
As an alternative you can
press Ctrl+H
In Search Mode pick Extended
Find - \r\n Replace - leave it empty.
^ is an anchor, that means it does not match characters (it matches the position after a \n, or the the start of the string). So nothing to replace.
If you need to use regex (aelors answer sounds good => +1), then
[\n\r]+
and replace with nothing or a space, according to your needs.
You can replace
[\r\n]+
with an empty string (or replace \n+ if you know your newlines are \n)
You can do the following:
Highlight all the lines you wish to join, then click "Ctrl" + "J"