I want to search for all occurences :
$elem->something
and replace it with
$elem['something']
Is there a way to search and replace using vscode by passing a variable ?
Sure,
you can search and replace using capturing groups
My first shot:
search: \$elem->(.*)\b
replace: $elem['$1']
Second shot:
search \$elem->([^ ]*)\b
replace: $elem['$1']
Make sure you enable the case sensitive and regular expression flags.
The result should be:
Related
I have multiple occurance of src={icons.ICON_NAME_HERE} in my code, that I would like to change to name="ICON_NAME_HERE".
Is it possible to do it with regular expressions, so I can keep whatever is in code as ICON_NAME_HERE?
To clarify:
I have for example src={icons.upload} and src={icons.download}, I want to do replace all with one regexp, so those gets converted to name="upload" and name="download"
Try searching on the following pattern:
src=\{icons\.([^}]+)\}
And then replace with your replacement:
name="$1"
In case you are wondering, the quantity in parentheses in the search pattern is captured during the regex search. Then, we can access that captured group using $1 in the replacement. In this case, the captured group should just be the name of the icon.
Let's say I have these lines in a file:
This and an child
That and a entrepreneur
These and a banana
This and an cookie
And I want to replace all an and a that are incorrect.
I can use an [^aeiou] or a ^[aeiou] to find all the incorrects (I think, I'm not good with regexp).
What I want is to replace the incorrects using these expressions without changing the letter after the a/an.
How can I do that?
You need to use a capturing group, (...), and then include that in the replacement
Search:
an ([^aeiou])
a ([aeiou])
Replace:
a $1
an $1
Here i'am trying to replace some text with some another text.
Example:-
almada-institute.com:25:info#almada-institute.com:info12345:"hello"<service#iubi.com>:nossl
in-graph.com:25:info#in-graph.com:info123456789:"hello"<service#iubi.com>:nossl
get-herbals.net:25:info#get-herbals.net:info321:"hello"<service#iubi.com>:nossl
I need to be like this
almada-institute.com:25:info#almada-institute.com:info12345:"hello"<info#almada-institute.com>:nossl
in-graph.com:25:info#in-graph.com:info123456789:"hello"<info#in-graph.com>:nossl
get-herbals.net:25:info#get-herbals.net:info321:"hello"<info#get-herbals.net>:nossl
For this i'am using :-
But it just replace the search content with the regular expression in replace field.
You can use regex groups. Based on your sample input/output above, the below worked for me.
Find what:
:(\w+#.*?):(.*?)<(\w+#.*?)>:
Replace with:
:\1:\2<\1>:
You can make regular expression groups by using ( and ) and reference then in replace using $N, which will access N-th group.
So in your case, you can do this
regex: ([^: ]+:[^:]+:([^:]+):[^:]+:[^:]+)(:[^: ]+)
replace: \1<\2>\3 or on some implementation $1<$2>$3
You can try it yourself here: https://www.regex101.com/r/hY8pY9/1
Use capturing groups. Putting another regex in their makes no sense. So with the screenshot you gave, the regex would be something like:
([a-z]\w+[#][a-z]\w+)[.]([a-z]\w+)
Replace with:
\1-\2.\1
I want to find and convert statements having arguements in some files.
Search statements are like:
db.AddInParameter(command, "#id", DbType.Int32, entity.Id);
Result statement is like:
command.Parameters.AddWithValue("#id", entity.Id);
I am using Notepad++ and trying regular expressions to create search string. I tried
db.AddInParameter*\"#+[a-z]\"*
db.AddInParameter*"#+[a-z]"*
But can not create the right search string.
Please suggest the correct the search and replace string.
Only change needed for me is adding start and end brackets
So, my replace string is:
\1.Parameters.AddWithValue\(\2, \3\);
Search: db.AddInParameter\((.*?), ("#[a-z]+"), .*?, (.*)\);
Replace: \1.Parameters.AddWithValue(\2, \3);
I have
2,5-3,5
3,5-4,5
...
and want
2.5-3.5
3.5-4.5
My "Find what" in Replace look like
[0-9],[0-9]
But I cannot make "Replace with"
\2.\1 to work. Nor does $2.$1.
This is clearly a simple task. What am I doing wrong?
Thanks
You need to specify groups to use replacement tags. I don't use notepad+, but if it's similar to other regex implementations \([0-9]\),\([0-9]\) should do the trick.
Did you checked Regular Expression checkbox(Search mode) in Notepad search and replace dialog?
I have just tried, this will work just fine for your case.
Search string: (\d),(\d)
Replace string: \1.\2
If you want to be more precise you can search and replace like this
Search string: (\d),(\d)-(\d),(\d)
Replace string: \1.\2-\3.\4
End just for reminder here is picture where you have to check regular expression option