VSCode - Is there any option to replace any form of text within single quotes in vs code? - visual-studio-code

I have a very long sql query
update emplyee_table SET name = 'abc',age='25',address='stackoverflow' WHERE id = 10
I want the above query to get replaced like this by any vscode operation.
update emplyee_table SET name = ?,age=?,address=? WHERE id = 10

You could use find and replace in VSCode along with an appropriate RegEx.
This Regex should work out: '[^';]*'

Ctrl+H will bring up the replace menu - after enabling regex replace (the button looks like .*), replace the pattern '[^';]*' with ?. Ctrl+Alt+Enter will replace all of the matches in the open editor.

Related

Find and replace all occurrences not in quotes

I'm in the middle of refactoring my Python code to use underscore notation as described in PEP8 and I am using VS Code's find-and-replace tool. The problem is occurrences of words in quotes are also being replaced. How can I replace all occurrences that aren't in quotes?
Example:
fooBar = df["fooBar"]
Desired after find and replace
foo_bar = df["fooBar"]
VS Code's Search functions supports regex, and you can use a negative lookbehind pattern to find "all fooBar strings that are not preceded by a quote": (?<!")fooBar.
(1) enables regex expressions when searching. As you can see, that ignores all the "fooBar" instances. Then you can also turn on (2) so that it enables whole word matching (so that it doesn't match "fooBar123").
As a side note, I hope you have automated tests you can run after doing all these renaming :)
open the find-and-replace vscode tool(ctrl+f and ctrl+h)
select the User Regular Expression(Alt+R)
select the Match Case (Alt+c)
Find = ([a-z]*)(?<!_)([A-Z])(?<!".*)
Replace = $1_\L$2
finaly press Enter
result is :

Is there a way to insert N times the same characters

I can't find if vscode has a such failure. Is there a way to construct a string with N characters?
I explain myselft:
I need to wrote an empty string like this:
foobar = "1111111111111111";
There is 16 times characters '1'. Is there a way, like in Vim, to construct the line like this:
i wrote 'foobar = "' then i'd make a command to repeat 16 times the character 'i'.
I hope it's clear for you.
Here is an easy way using HyperSnips - a snippet extension that can use javascript to produce the output. First the demo:
The HyperSnips snippet:
snippet `"(.+)\*(\d+)=` "expand" A
``
let origStr = m[1];
let howMany = parseInt(m[2]);
let newStr = origStr.repeat(howMany);
rv=`"${newStr}`
``
endsnippet
This code goes inside a <yourLanguage>.hsnips file to have it run only in that language or all.hsnips to run in all files.
I made it to run inside a "" using this key: (.+)\*(\d+)=
The = is really the trigger - it runs automatically - you could change that to be something else. [The key could be shorter if you didn't care about digits being repeated.]
For more on setting up HyperSnips (which is pretty easy) see VSCode Advanced Custom Snippets
There currently is no native way, outside of copy/pasting.
You can use Repeat Paste:
Copies selected text and pastes repeatedly based on user input. Functions like Vim yank, paste, and repeat. For example, in Vim you would select the characters you want to copy then type 30p to paste the selected text 30 times.
Select a char and activate your command palette with CTRL + SHIFT + P and type 'Repeat Paste' and it will prompt you for the quantity.
You can assign a shortcut to this command
You can use the extension Regex Text Generator. You write a Regular Expression that generates your needed text
Type the following in the Generate Box
foobar = "1{16}";

vscode multicursor reorder lines how to

I am finding a common issue I have with vscode multi-cursor editing mode is how to deal with "fields" of different lengths. To give an example:-
I have an sql file which contains multiple lines of the form
INSERT INTO settings (name,value) VALUES('key_for_value', 'value_to_set'); -- a comment about this setting
Which I want to turn into
UPDATE settings SET name = 'value_to_set' WHERE name = 'key_for_value'; -- a comment about this setting
Obviously each 'key_for_value' and 'value_to_set' are different lengths in each line.
Its fairly easy to grab a bit of the first line of INSERT INTO... and with a control-D get multiple cursors from there, and edit the lines to to get to UPDATE settings SET name = but then I am stuck - I can't find a key binding to (for instance) jump to the next comma - then start a selection and jump the next closing round bracket. I could then do a multiline cut, move the cursor back to start of line, more forward to after the = sign and do a multiline paste.
Is this sort of thing possible?
you can do it with a regex search replace
Search for
INSERT INTO (\w+) \((\w+),(\w+)\) VALUES\('([^']+)', '([^']+)'\);(.*)
replace with
UPDATE $1 SET $2 = '$5' WHERE $2 = '$4';$6

Find and replace using wildcard in netbeans

How would one go about using replace in netbeans to modify a PHP script with lots of $_POST[].
For better security it would be a good idea to replace all these $_POST[]
with sanitize($_POST[]) where sanitize is a function that sanitizes user input.
So I could use Replace and search for $_POST[''] and replace with sanitize($_POST['']).
But how do you keep the variable name within each $_POST[''] while adding the closing parenthesis?
For example $_POST['name'] and $_POST['action'] need to become sanitize($_POST['name']) and santize($_POST['action']) respectively.
I am not a PHP programmer or a Regex master but in my very limited test this seemed to work.
Select the project and then choose Edit -> Replace. Choose "Regular Expression" in the drop down list, set containing text to \$_POST\[(.*)\]and replace with to sanitize(\$_POST[$1])

How to search and replace 2 lines (together) in Eclipse?

I would like to search multiple files via eclipse for the following 2 lines:
#Length(max = L_255)
private String description;
and replace them with these two:
#Length(max = L_255, message="{validator.description.len}")
private String description;
Another tip on how to get the regex for a selected block.
Open one of the files that contains the multiple lines (multiline) to search or replace.
Click Ctrl+F and select "Regular expression". Close the Find/Replace window.
Select the block you need and click again Ctrl+F to open the Find/Replace window.
Now in the Find text box you have the regular expression that exactly matches your selection block.
(I discovered this, only after creating manually a regexp for very long block :)
Search are multi-line by default in Eclipse when you are using regex:
(\#Length\(max = L_255)\)([\r\n\s]+private)
I would like to add "private String description;"
(\#Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)
replaced by:
\1, message="{validator.description.len}")\2
It works perfectly in a File Search triggered by a CTRL-H.
As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexp for you by Eclipse.
CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).
Select your lines.
Click CTRL+H. The search dialog opens up.
If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search.
If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choice).
Select your lines again.
Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.
A quick tip for including multiple lines as part of a manually constructed regular expression:
Where you would normally use .* to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ? at the end to make it non-greedy.
Explanation: . doesn't match new lines so need to do an "either-or": The parentheses match either the . before the pipe or the new line after it. The ? after \r makes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?: excludes the whole thing as a capturing group (which helps to avoid a stack overflow).
Click Ctrl + F and select "Regular Expression" and then search the lines. In case to perform the same on multiple files, click Ctrl + H, click on 'File Search' and perform the same.
Select the folder that contains all your files and press Ctrl+H.