Sublime text regex find and replace - regex

In sublime text, I need to replace all instances of:
rs('???????')
with
$rs['??????']
(keeping the ??????? part the same). I'm a regex virgin and I've only got as far as locating the regex button on the find and replace panel.
How is this possible.

Find regex:
rs\('(.*?)'\)
Replace:
$rs['$1']
Beware that this will expect anything inside the quotes; if it isn't what you need, please tell me :)

Related

How to replace all lines based on previous lines in Notepad++?

I have an XML code:
<Line1>Matched_text Other_text</Line1>
<Line2>Text_to_replace</Line2>
How to tell Notepad++ to find Matched_text and replace Text_to_replace to Replaced_text? There are several similar blocks of code, with one exactly Matched _text and different Other_text and Text_to_replace. I want to replace all in once.
My idea is to put
Matched_text*<Line2>*</Line2>
in the Find field, and
Matched_text*<Line2>Replaced_text</Line2>
in the Replace field. I know that \1 in regex might be useful, but I don't know where to start.
The actual code is:
<Name>Matched_text, Other_text</Name>
<IsBillable>false</IsBillable>
<Color>-Text_to_replace</Color>
The regex you're looking for is something like the following.
Find: (Matched_text[\w,\s<>\/]*<Color>-).*(</Color>)
Replace: \1Replaced_text\2
Broken down:
`()` is how you tell regex that you want to keep things (for use in /1, /2, etc.), these are called capture groups in regex land.
`Matched_text[\w,\s<>\/]*` means you want your anchor `Matched_text` and everything after it up till the next part of the expression.
`<Color>-).*(</Color>)` Select everything between <Color>- and </Color> for replacement.
If you have any questions about the expression, I highly recommend looking at a regex cheatsheet.

Simple regex: turn $text$ to \(text\) in Sublime Text

In my work I have a very repetitive task, and I would believe that regex is a fast solution for it, though, my knowledge of regex is very sparse.
What I want to do is to in a txt-file find all occurences of
$text$
and replace them with
\\( text \\)
where text is LaTeX-code, so it can contain alphanumeric as well as other characters.
I plan to do this in Sublime Text, as it has a built in and convenient regex engine. Can anyone help?
Regex:
\$([^$]*)\$
REplace With:
\\\\( \1 \\\\)

Remove text appearing after numbers in Notepad++ using regular expressions

I have a large text file which contains many timestamps. The timestamps look like this: 2013/11/14 06:52:38AM. I need to remove the last two characters (am/pm/AM/PM) from each of these. The problem is that a simple find and replace of "AM" may remove text from other parts of the file (which contains a lot of other text).
I have done a find using the regular expression (:\d\d[ap]m), which in the above example would track down the last bit of the timestamp: :38AM. I now need to replace this with :38, but I don't know how this is done (allowing for any combination of two digits after the colon).
Any help would be much appreciated.
EDIT: What I needed was to replace (:\d\d)[ap]m with \1
Make (:\d\d[ap]m) into (:\d\d)[ap]m and use $1 not \1
Go to Search > Replace menu (shortcut CTRL+H) and do the following:
Find what:
[0-9]{2}\K[AP]M
Replace:
[leave empty]
Select radio button "Regular Expression"
Then press Replace All
You can test it at regex101.
Note: the use of [0-9] is generally better than \d (read why), and avoiding to use a capture group $1 with the use of \K is considered better. It's definitely not important in your case, but it is good to know :)

Regex substitution with Notepad++

I have a text file with several lines like these ones:
cd_cod_bus
nm_number_ex
cd_goal
And I want to get rid of the - and uppercase the following character using Notepad++ (I can also use other tool but if it doesn't get the problem more troublesome).
So I tried to get the characters with the following regex (?<=_)\w and replace it using \U\1\E\2 for the uppercasing trick but here is where my problems came. I think the regex is OK but once I click replace all I get this result:
cd_od_us
nm_umber_x
cd_oal
as you can see it is only deleting the match.
Do you know where the problem is?
Thanks.
The search regex has no capture groups, i.e. the \1 and \2 references in the replacement do not refer to anything.
Try this instead:
Search: _(\w)
Replace \U\1\E
There you have a capture group in the search part (the parenthesis around the \w) and the \1 in the replacement refers back to what was captured.
replace
_(.)
with
\U$1
will give you:
cdCodBus
nmNumberEx
cdGoal
and for your
I can also use other tool but if it doesn't get the problem more troublesome
I suggest you try vim.
Try this,
_(\w)
and replace with
\U\1
here's a screenshot

Replacing rgb from multiple lines with a one line version

I want to convert:
<r>240</r>
<g>240</g>
<b>240</b>
to:
rgb="240,240,240"
using Notepad++. I've tried
<r>(\d+)</r>$<g>(\d+)</g>$<b>(\d+)</b>
but it didn't work. I have about 20+ files containing multiple occurrences of this and would appreciate any help I can get. Can someone explain what I'm doing wrong please?
Notepad++ won't do a multiline regex. You first have to replace all of the newlines in the file. You can then do a regex replace with
regex: <r>(\d+)</r><g>(\d+)</g><b>(\d+)</b>
replace: rgb="\1,\2,\3"\r\n
Or you can break it up into more steps and do each line individually.
regex: <r>(\d+)</r>
replace: rgb="\1,
then
regex: <g>(\d+)</g>
replace: \1,
etc...
I just tested this on my system, I highlighted the tags then did search->Replace make sure you have checked Match Whole Word Only and Wrap around. worked for me replacing the text in multiple files.