Regex Transform or Text Replace in Text Editor - regex

I am trying to do an advanced text search and replace in a text editor (I can use SublimeText or VSCode)
Input:
parameters['myParameter1']
parameters['myParameter2']
Expected output:
myParameter1
myParameter2
And I have hundreds of similar scenario in the file. So that's why I'm thinking of using regex to solve this.
Thanks

Hope this will help you out.
Regex demo
Regex search: parameters\['([^']+)'\]
1. parameters\[' this will match parameters[' Optinally if you have other keywords then parameters and which can be dynamic for that you can use [a-zA-Z]+ this will include all lower case and upper case.
2. ([^']+) this will match all except ', () will capture first match in $1.
3. '\] this will match ending ']
Replacement: $1
Note: If you are using gedit ubuntu you have to replace it with \1

VSCode can search all files in a directory, and it supports RegExp too!
Use ctrl+shift+f to search all files, hit the .* icon for RegExp.
A search expression such as parameters['(.*)'] replaced with $1 should work!
Check out the official guide for more info: VSCode Basic Editing

Related

Add word to the end of a each line in all files using regex notepad++

I have 10 files of 100 lines each. I need to do their translation.
This is one line in some file: "Client Notes,141"
and another simular word line in another file "Client Notes,700"
I want to modify all the concerned lines in all the 10 files to be:
"Client Notes,141,KundLinjer"
"Client Notes,700,KundLinjer"
"Client Notes,770,KundLinjer"
I tried with regular expressions and macros but I couldn figure it out
Thanks for help!
Assuming it is Notepad++ and not Notepad2:
Ctrl+H
Find what: \bClient Notes,\h*\d+\K
Replace with: ,KundLinjer
check Wrap around
check Regular expression
Replace all
Explanation:
\b : word boundary
Client Notes, : literally
\h* : 0 or more horizontal spaces
\d+ : 1 or more digits
\K : forget all we have seen until this position
Result for given example:
Client Notes,141,KundLinjer
Client Notes,700,KundLinjer
You may try the following find and replace:
Find:
^Client Notes,(\\d+)
Replace:
Client Notes,$1,KundLinjer
To make it apply to multiple files, use the directory option in the dialog to select the folder which contains the 10 files. If the 10 files be scattered in multiple places, then create a single folder containing those files. Also, make sure you do the find and replace with regex mode enabled.
In nodepad++ you can use the shortcut: Ctrl+H and under the Replace tab, search mode select Regular expression
Find:
(Client Notes, \d+)
Replace:
\1, KundLinjer
You have the option to:
Replace All in All Opened Documents.
Explanation
The brackets (client Notes, \d+) will 'capture' anything within the brackets to be used in our replacement \1 (if you had more captures, you could use \2, \3 etc..)
\d+ means any digit, one or more times (in your case 141, 700).
So we are replacing the text "Client Notes (AnyNumber)" with "Client Notes (AnyNumber) KundLinjer".
You could also replace (.*) with \1 KundLinjer if you want to add KundLinjer to all lines, no matter what.

Notepad++ replace text with RegEx search result

I would like replace a standard string in a file, with another that is a result of a regular expression. The standard text looks like:
<xsl:variable name="ServiceCode" select="###"/>
I would like to replace ### with a servicecode, that I can find later in the same file, from this URL:
<a href="/Services/xyz" target="_self">
The regular expression (?<=\/Services\/)(.*)(?=\" )
returns the required service code "xyz".
So, I opened Notepad++, added "###" to the "Find what" and this RegEx to the "Replace with" section, and expected that the ### text will be replaced by xyz.
But I got this result:
<xsl:variable name="ServiceCode" select="?<=/Services/.*?=" "/>
I am new to RegEx, do I need to use different syntax in the replace section than I use to find a string? Can someone give me a hint how to achieve the required result? The goal is to standardize tons of files with similar structure as now all servicecodes are hardcoded in several places in the file. Thanks.
You could use a lookahead for capturing the part ahead.
Search for: (?s)###(?=.*/Services/([^"]+)") and replace with: $1
(?s) makes the dot also match newlines (there is also a checkbox available in np++)
[^"] matches a character that is not "
The replacement $1 corresponds to capture of first parenthesized subpattern.
I am no expert at RegEx but I think I may be able to help. It looks like you might be going at this the wrong way. The regex search that you are using would normally work like this:
The parenthesis () in RegEx allow you to select part of your search and use that in the replace section.
You place (?<=\/Services\/)(.*)(?=\" ) into the "Find what" section in Notepad++.
Then in the "Replace with" section you could use \1 or \2 or \3 to replace the contents of your search with what was found in the (?<=\/Services\/) or (.*) or (?=\" ) searches respectively.
Depending on the structure of your files, you would need to use a RegEx search that selects both lines of code (and the specific parts you need), then use a combination of \1\2\3 etc. to replace everything exactly how it was, except for the ### which you could replace with the \number associated with xyz.
See http://docs.notepad-plus-plus.org/index.php/Regular_Expressions for more info.

replace text with regular expression keeping structure match on sublime text

i been trying a few options, but i can't figure out.
this is the text i'm looking for:
php[whatever_is_in_between]myfunction
and i want to change it to this
=[whatever_is_in_between]myfunction
where [whatever_is_in_between] = \n or \n\t or \n\t\t or \nbarspace or \nbarespace\t and so on
so i found this regexp match the search text:
php[\n]*[\t]*[ ]*myfunction\(
this is the "replace with" text:
=[\n]*[\t]*[ ]*myfunction\(
but the regexp does not work on the replacement, it replace it as text.
can anybody help me with this?
thanks
I think the problem is that you are not using a capturing group ( ). Among other things, a capturing group allows you to take input from the the read text and then inject it into your replacement text.
I'd use a search pattern like this:
php\[([^\]]*)\]([\w\W]*)
It looks complicated, but I've set up a sample on Regex 101 that you can check out. The replacement text should look something like this:
[\1]\2
Please note that how you insert a capturing group will depend on what programming language you're using. The above should work for php.
I hope that helps,
--Jonathan

Replace text with regular expressions in a text editor

I need to edit lines in a text file.
The text files contains more than 100 lines of data in the below format.
Cosmos Rh Us (Paperback) $10.99 Shipped:
The Good Earth (Paperback) $6.66 Shipped:
BEST OF D.H. LAWRENCE (Paperback) $7.89 Shipped:
...
These are excerpts from the online book shop I use to buy books
I have this data in a test editor. How do I edit it [Fine/Replace] such that the data becomes like this
$10.99
$6.66
$7.89
or better, without the dollar sign, since it'll be easy total it.
I use notepad++ as text editor.
Search for (don't forget to enable regular expressions in the replace box!)
^.*\$(\d+\.\d+).*$
and replace all with
\1
You could simply match full lines and capture all numbers after the $ sign:
Find what: ^[^$]*\$(\d+\.\d+).*$
Replace with: $1
Make sure that you don't check the ". matches newline" option. And note that this will behave unexpectedly if you have multiple $ signs in a line.
You might need to update to Notepad++ 6. Before that some regex features were not working properly.
Find:
((?<=\$)[\d\.]+)
Replace With:
\1 or $1 (whichever Notepad++ uses)
first regex will be replaced with nothing
[a-zA-Z0-9].*\)
second regex will be replaced with nothing
[a-zA-Z]+\:

How do I access matched objects for replacement when using regular expression mode in PL/SQL Developer Find & Replace?

I have a query where I want to replace
avg(j2)
with
avg(case when j2 <> 0 then j2 else 0 end)
The above is a specific example but the pattern is the same with all the replacements. It's always a word followed by a number that needs to be replaced with the case statement that checks if the number is not 0.
I tried the following for find:
avg(\(\w\d\))
and the find works. Now, I want to do a replace so I try:
avg(case when \1 <> 0 then \1 else 0 end)
but it puts literal \1 and not the captured text from the match. I tried \\1 & $1 as well and it takes all of them literally. Can anyone tell me what the right syntax is for using the captured text for replacement? Is this supported?
Thanks,
Ashish
I am not sure if the PL/SQL Developer IDE supports group capture. The recent versions do seem to support regex based find and replace though. Cant find a source to confirm if group capture works.
Why dont you try pasting the code in a something like Notepad++ and try the same regex. It should work. You could paste the result back to your IDE and continue from there...
You can replace it using $ and number like,
$0 or $1 etc. see an example below
find: TABLE (.*\..*) IS
replace: COLUMN $1 IS
http://regexr.com/3gm6c