replace text with regular expression keeping structure match on sublime text - regex

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

Related

Remove all text except a certain string in Notepad++

I'm extracting case numbers from a wall of text. How do I filter out all the useless text using the replace function in Notepad++ with the help of RegEx? The parts I want to keep are made up of letters, digits, and a hyphen (SPP-1803-2045227).
I would like to turn this...
(SPP-1803-2045227)Useless text goes here. 2019-05-18 *
(SPP-1915-1802667)More useless text. 2019-01-14 *
(SPP-1904-1012523)And some more. 2019-02-03 *
...into this:
SPP-1803-2045227
SPP-1915-1802667
SPP-1904-1012523
I've been playing around with RegEx and also found something in another thread on here before, which wasn't the solution but came very close. Unfortunately I can't find it anymore. It looked something like this:
^(?!S\w+).*\r?\n?
Any help is appreciated.
you could try something like this.
find: .*\((\w{3}-\d{4}-\d{7})\).*
replace with: \1
The above Regular Expression matches the whole line with your letters and digits between an extra pair of parentheses.
When you replace with \1 you keep only the match between parentheses.
Remember to select Regular Expression Search mode.

Text replace challenge (regex)

I can't solve a problem. Perhaps it is impossible to achieve what I want.
GOAL: use only replace function to remove all text except the email address.
I have a text with email in: Start text some other text 2828 text my.address#mail.com some additional text.
Regular expression to select email: [a-zA-Z0-9\-\._]+#[\w\d\-\._]+\.\w{2,12}
Regular expression works perfectly to find an email address, but it didn't work to remove all letters from an email.
Below print screen shows what I got as a result when apply replace function in the text editor:
As results I used regexp .*([a-zA-Z0-9\-\._]+#[\w\d\-\._]+\.\w{2,12}).*, and replace it on $1. Sadly this workflow give me broken email.
I used email as an example, the same result I got for any other data types as URLs, IPs, phones, names, cities, zips etc.
Can anyone unveil a solution to this problem?
Thank you a lot.
PS I am not interested in using math() function, because of this function isn't presented in most of the text editors.
I think you should make the first part non greedy .*? or else the .* will match upon the # and after that just giving up 1 match to satisfy the character class [a-zA-Z0-9\-\._]+
If it is not greedy it will capture my.address#mail.com instead of s#mail.com
.*?([a-zA-Z0-9\-\._]+#[\w\d\-\._]+\.\w{2,12}).*
I would do it like this:
Find: (.*?)[a-zA-Z0-9\-\._]+#[\w\d\-\._]+\.\w{2,12}\s?(.*?)
Replace: $1$2
Input: Start text some other text 2828 text my.address#mail.com some additional text
Output: Start text some other text 2828 text some additional text

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.

multi-line xml regex in sublime

I have a large logfile (+100 000 lines) in XML like so:
<container>
<request:getApples xml="...">
...
</request:getApples>
<request:getOranges xml="...">
...
</request:getOranges>
</container>
...
I want to extract the :getXXXX part to
getApples
getOranges
by doing a regex find & replace in Sublime Text 2.
Something like
Find: [^(request:)]*(.*) xml
Replace: $1\n
Any regex masters that can assist?
Correcting mart1n's answer and actually using ST2 and your sample input, I came up with the following:
First, CtrlA to select all. Then, CtrlH,
Search: .*?(get\w+) .*
Replace: $1
Replace All
Then,
Search: ^[^get].*$
Replace: nothing
Replace All
Finally,
Search: ^\n
Replace: nothing
Replace All
And you're left with:
getApples
getOranges
Not familiar with Sublime Text but you can do in two parts:
Find .*?\(get\w+\) .* and replace with \1. Now those get* strings are on separate lines with nothing else. All that remains is to remove the cruft.
So, many ways to do this. Easy one: find ^[^g][^e][^t].*$ and replace with nothing (an empty string).
Now you have your file that contains just the string you want and some empty lines, which (I hope) Sublime can get rid of with some delete-empty-lines function.
You can quickly throw all of the above in a macro and execute at will for any input following the same format ;-)
If you're willing to take the problem out of sublime text, you can use the dotall flag along with lazy matching to extract only the getXXX parts.
Replacing
.*?(get\w*) .*?
with
$1\n
should get you most of the way, only leaving a bit of easily removeable closing tags at the end of the file that I can't figure out at present.
You can check this solution here.
Maybe someone could take this and figure out a way to remove the extra closing tags.
Try this
Find what: :(\w+)>|.\s?
Replace with: $1
And if didn't work as intended, then let me know?

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]+\: