How do I join two regular expressions into one in Notepad++? - regex

I've been searching a lot in the web and in here but I can't find a solution to this.
I have to make two replacements in all registry paths saved in a text file as follows:
replace all asterisc with: [#42]
replace all single backslashes with two.
I already have two expressions that do this right:
1st case:
Find: (\*) - Replace: \[#42\]
2nd case:
Find: ([^\\])(\\)([^\\]) - Replace: $1$2\\$3
Now, all I want is to join them together into just one expression so that I can do run this in one time only.
I'm using Notepad++ 6.5.1 in Windows 7 (64 bits).
Example line in which I want this to work (I include backslashes but i don't know if they will appear right in the html):
HKLM\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\
I already tried separating it with a pipe, like I do in Jscript (WSH), but it doesn't work here. I also tried a lot of other things but none worked.
Any help?
Thanks!
Edit: I have put all the backslashes right, but the page html seem to be "eating" some of them!
Edit2: Someone reedited my text to include an accent that doesn't remove the backslashes, so the expressions went wrong again. But I got it and fixed it. ;-)
Sorry, but this was my first post here. :)

As everyone else already mentioned this is not possible.
But, you can achieve what you want in Notepad++ by using a Macro.
Go to "Macro" > "Start Recording" menu, apply those two search and replace regular expressions, press "Stop Recording", then "Save Current Recorded Macro", there give it a name, assign a shortcut, and you are done. You now can reuse the same replacements whenever you want with one shortcut.

Since your replacement strings are totally different and use data that come not from any capture (i.e. [#42]), you can't.
Keep in mind that replacement strings are only masks, and can not contain any conditional content.

Related

Search and replace with particular phrase

I need a help with mass search and replace using regex.
I have a longer strings where I need to look for any number and particular string - e.g. 321BS and I need to replace just the text string that I was looking for. So I need to look for BS in "gf test test2 321BS test" (the pattern is always the same just the position differs) and change just BS.
Can you please help me to find particular regex for this?
Update: I need t keep the number and change just the text string. I will be doing this notepad++. However I need a general funcion for this if possible. I am a rookie in regex. Moreover, is it possible to do it in Trados SDL Studio? Or how am i able to do it in excel file in bulk?
Thank you very much!
Your question is a bit vague, however, as I understand it you want to match any digits followed by BS, ie 123BS. You want to keep 123 but replace BS?
Regex: (\d+)BS matches 123BS
In notepad++ you can:
match (\d+)BS
replace \1NEWTEXT
This will replace 123BS with 123NEWTXT.
\1 will substitue the capture group (\d+). (which matches 1 or more digits.
You could do this in Trados Studio using an app. The SDLXLIFF Toolkit may be the most appropriate for you. The advantage over Notepad++ is that it's controlled and will only affect the translatable text and not anything that might break the integrity of the file if you make a mistake. You can also handle multiple files, or even multiple Trados Studio projects in one go.
The syntax would be very similar to the suggestion above... you would:
match (\d+)BS
replace $1NEWTEXT

How to use a regular expression in notepad++ to change a url

I need some help with our migrated site urls's. We moved our site from Joomla to Worpdress and IN our posts we have over 20K of internal links.
The structure of these links are like these:
www.mysite.nl/current-post-title/index.php?option=com_content&view=article&id=5259:related-post-title&catid=35:universum&Itemid=48
What we need is this:
www.mysite.nl/related-post-title
So basically we need to remove everyhing behind www.mysite.nl/ up until the colon :, i.e. remove this: current-post-title/index.php?option=com_content&view=article&id=5259: (must remove the colon itself too)
And then remove everything behind the first ampersand (including the ampersand itself) until the end of the string, i.e. remove &catid=35:universum&Itemid=48
Of course only url strings containing this index.php?option=com_content must be changed.
I have dumped the table in plain text and opened it in Notepad++ to do a search and replace with regular expression because the content that must be removed from these lines is different every time.
Can someone please help me with the right regular expression?
In find what box enter below:
(www.mysite.nl)\/.*index.php\?option=com[^:]+:([^&]+)&.*
In replace with box enter:
\1/\2
Result
www.mysite.nl/related-post-title
Go inside-out, rather than outside-in, replace \/.+&id=\d+\:(.+?)&.+ with /$1. Also, paste a few into http://www.regexr.com/ and play around, although JavaScript and Notepad++ might have some differences in implemented Regex features, e.g. negative lookbehinds.

Notepad++ Regex - Finding and replacing multiple different criteria simultaneously

I've just started to get to grips with regex in notepad++ and I've tasked myself with formatting a chunk of JSON data into something human readable, as well as something that can be read into an algorithm a colleague of mine wrote. I've found a few regex expressions that do this perfectly, but in order to get to my desired result, I have to do it in four separate Find/Replace steps. Is there some sort of way I can create one single find/replace expression that handles all of the above tasks for me?
Currently I have Notepad++ doing the following:
Deleting all quotation marks by finding " and replacing it with
nothing
Deleting all commas by finding , and replacing it with nothing
Changing all underscored numbers that are followed by a colon with
the number 0 (the reason behind this is particular to the project)
by finding _[0-9]*: and replacing with _0 and finally, putting all
of a particular expression onto it's own line by finding the start
of the particular string I'm after and adding \n.
I know that's convoluted, but fortunately it does the job. Is there any way of consolidating all that into a single command, or does that all have to be done step by step?
Thanks guys :)
Notepad ++ allows you to consolidate individual search and replaces as a macro which you can also save.
Hit the record button in the toolbar (or Macro>Start Recording)
perform these regex replacements in the required order.
hit stop button in toolbar (or Macro>Stop Recording)
Hit the play button to perform all the required replacement operations again.
Save the macro by going into the Macro option in the window menu and 'save current recorded macro'
As for the first to replacements you could use the following expression: (?:"|,)

How to effectively search and replace in Vim by first "testing" or "preview" the search part?

Sometimes I want to search and replace in Vim using the s/search_for/replace_with/options format, but the search_for part becomes a complicated regex that I can't get right the first time.
I have set incsearch hlsearch in my .vimrc so Vim will start highlighting as I type when I am searching using the /search_for format. This is useful to first "test"/"preview" my regex. Then once I get the regex I want, I apply to the s/ to search and replace.
But there is two big limitation to this approach:
It's a hassle to copy and paste the regex I created in / mode to s/ mode.
I can't preview with matched groups in regex (ie ( and )) or use the magic mode \v while in /.
So how do you guys on SO try to do complicated regex search and replace in Vim?
Test your regex in search mode with /, then use s//new_value/. When you pass nothing to the search portion of s, it takes the most recent search.
As #Sam Brink also says, you can use <C-r>/ to paste the contents of the search register, so s/<C-r>//new_value/ works too. This may be more convenient when you have a complicated search expression.
As already noted, you can practice the search part with /your-regex-here/. When that is working correctly, you can use s//replacement/ to use the latest search.
Once you've done that once, you can use & to repeat the last s/// command, even if you've done different searches since then. You can also use :&g to do the substitute globally on the current line. And you could use :.,$&g to do the search on all matches between here (.) and the end of the file ($), amongst a legion of other possibilities.
You also, of course, have undo if the operation didn't work as you expected.
As the others have noted I typically use s//replacement/ to do my replacements but you can also use <C-r>/ to paste what is in the search register. So you can use s/<C-r>//replacement/ where the <C-r>/ will paste your search and you can do any last minute changes you want.
<C-r> inserts the contents of a register where the cursor is
The / register holds the most recent search term
:registers will display the contents of every register so you can see whats available.
Since Neovim 0.1.7 there is the Incremental (“live”) :substitute function. (so this only works in Neovim!)
To enable it set the incommand option:
:set inccommand=split
It was announced here: https://neovim.io/news/2016/11/

How can I match the beginning of a line in dreamweaver with regex?

Problem:
^.+ matches only the first line of the source code in dreamweaver. I need it to match each line so that I can wrap each full line in P tags. I have 500 files to do this in.
I know ^ should match the beginning of a line and I also know that multi-line mode must be enabled for it to work on each line and not just at the beginning of the file. I also know dreamweaver uses javascript source code.
Is lack of multi-line mode the problem? Is there any way to turn it on in dreamweaver? I tried using /m at the beginning search to enable multi-line mode, but that didn't work either.
I'm open to any solution for my current problem, even if it involves a different program. However, a fix for dreamweaver is ideal, 2nd place is a way to do this in notepad++, 3rd place is a way do to this in python or something (I only know javascript, you'll have to spell it out exactly in another language).
Thank you,
robert
p.s.
I found I can "select all > right click > selection > indent" to add two spaces to the beginning of each line in dreamweaver. This allows me to find the beginning of each line with / {2,}/. I really don't want to select all > indent on all 500 files, but i'm about to start since I've already spent a few hours bludgeoning dreamweaver.
Don't use Dreamweaver for this - use Notepad++ (since you are familiar with it) at its regular expression support is superior.
If you are comfortable with a more robust scripting language (Python, Ruby, Perl, etc.) then that would be an ever better way to do it.
The way that I might do this in DW would not involve using the find-replace tool's "Regular Expression" option, but instead using just plain old matching on a CrLf.
In the Find portion, since you can't directly enter a CrLf, you'll have to copy one to your clipboard beforehand and paste it in where needed.
In the Replace portion, replace with:
</p>[CrLf]
<p>
Again, be sure to paste in a proper "[CrLf]". This will work on all but the very first and very last lines of your document, so I know this isn't a 100% solution. There are probably better solutions using other tools that someone else can recommend!
Good luck!
-Mike
I had a flash of insight right after posting. (isn't that the way of it?)
Dreamweaver can find the end of each line with \r\n so instead of trying to work forward, i should have just worked backwards.
search: (.+)(\r\n)
replace: <p>$1</p>$2
[\w\W]* matches anything, including a newline. Its greedy, so it fact it matches everything.