Different between regex in "find" box and "replace" box, Eclipse - regex

I'm trying to do a find/replace in Eclipse using the following pattern: \$.*}.
If I put this into the "find" search box in Eclipse I can find all the matches in a file (e.g. ${MASTER_KEY}). However, I'm not sure what to put in the "replace" box. My goal is to replace ${MASTER_KEY} with $'{MASTER_KEY}'. I tried '\$.*}', but that didn't work.

Search By this:
\$\{(.*?)\}
and replace by this:
\$'\{\1\}'
Explanation:
{.*} will match "${abc}pqr${def}" as a single match which will be wrong in your case. Whereas .*? makes it lazy thus it will match ${abc} and ${def} separately which you need.

Related

Replace wrong xml-comments with Regex

I am dealing with a bunch of xml files that contain one-line-comments like this: // Some comment.
I am pretty sure that xml comments look like this: <!-- Some comment -->
I would like to use a regular expression in the Atom editor to find and replace all wrong comment syntax.
according to this question, the comment can be found with (?<=\s)//([^\n\r]*) and replaced with something like <!--$1-->. There must be an error somewhere since clicking replace button leaves the comment as is, instaed of replacing it. Actually I can't even replace it with a simple character.
The find and replace works with a different regex in the "Find" field:
Find: name.*
Replace: baloon
Is there anything I can write in the "Find" and "Replace" field to achieve this transformation?
Atom editor search and replace currently does not support lookbehind constructs, like (?<=\s). To "imitate" it, you may use a capturing group with an alternation between start of string, ^, and a whitespace, \s.
So, you may use
Find What: (^|\s)//([^\n\r]+)
Replace With: $1<!--$2-->
See the regex demo. NOTE \s may match newlines, so you may probably want to use (^|[^\S\r\n])//([^\n\r]+) to avoid matching across line breaks.
If you do not need to check for a whitespace, just remove that first capturing group and use a mere:
Find What: //([^\n\r]+)
Replace With: <!--$1-->
See another regex demo.

Regex and replace selected results only

I want to know whether there is any tool that does a regex search across huge text (xml or tagged or html) and replace only the cases which are selected across shown (should have 'select/deselect/select all' options while applying the replace regex).
Like the below example:
My content is:
"Visited xtreme.com, stupid.net, childish.com, happy.net and innocence.edu. There are some cross.network isssues that are to be fixed."
Now in this content, i want to replace all ".net" occurrences with ".com" and so a simple tool like notepad++ would replace it easily. But i want the tool to show the search results and give the option to replace only first two occurrences of ".net" and not the instance in "cross.network"
This is only example purposes only and don't suggest an alternate regex. I don't need it.
NP++ or sublime are all fine, as long as they can read all the text to the memory. They both support regexes for finding and replacing text.
If the text files are too big, i.e. NP++ crashes, then you can use sed. It's a command line tool that can replace text like this:
sed -i filename.txt 's/pattern/replacement/g'
On windows boxes you need mingw or cygwin to run it.
Use a text editor like sublime and apply a word boundary to the regular expression:
\.net\b
This will find .net in stupid.net but not in cross.network.
See a demo on regex101.com.

Need help using RegEx to Replace lines

I need to do a Find & Replace (NOT in Code, in the VB IDE or a Text Editor).
I need to take:
IsNumeric(CInt(whatevertextishere))
and change it to
IsNumeric(whatevertextwasthere)
I've tried:
IsNumeric\(CInt\(.*\)\)
for find, and IsNumeric($1) for the replace, the find works, but the replace does not. It simply replaces it with the literal text IsNumeric($1), instead of putting the matched text, which should be the "whatevertextishere".
What am I doing wrong?
You haven't defined a capture group in the regex (with unescaped parentheses):
IsNumeric\(CInt\((.*)\)\)

VisualStudio's QuickReplace finds it with RegEx but won't replace it

I'm trying to replace parts within my code that looks like this
Class.Method<>("SomeKeyHere"]
Notice the square bracket at the ending - that's what I want to replace with the correct bracket.
My RegEx to find it looks like this:
Class\.Method\<\>\("{[^"]+}"\]
This RegEx seems to find the occurences pretty well.
My RegEx I want to use to replace (with the correct bracket at the end) is this:
Class\.Method\<\>\("(\1)"\)
However, VS is finding everything using Quickfind or Quickreplace's Find button but it won't replace it, telling me it hasn't found any occurences
The Problem was, that Visual Studio seems not to understand, that the "Replace with"-Part contained RegEx.
The Regex that worked was:
Class.Method<>(\"\1\")
Simpler, than I thought. Seems like VisualStudio only understands the \1, \2,.. Parts within the "Replace with" field.

Multiline Regular Expression search and replace!

I've hit a wall. Does anybody know a good text editor that has search and replace like Notepad++ but can also do multi-line regex search and replace? Basically, I am trying to find something that can match a regex like:
search oldlog\(.*\n\s+([\r\n.]*)\);replace newlog\(\1\)
Any ideas?
Notepad++ can now handle multi line regular expressions (just update to the latest version - feature was introduced around March '12).
I needed to remove all onmouseout and onmouseover statements from an HTML document and I needed to create a non-greedy multi line match.
onmouseover=.?\s*".*?"
Make sure you check the: [ ] . matches newline checkbox if you want to use the multi line match capability.
EditPad Pro has better regex capabilities than any other editor I've ever used.
Also, I suspect you have an error in your regex — [\r\n.] will match only carriage returns, newlines, and full stops. If you're trying to match any character (i.e. "dot operator plus CR and LF), try [\s\S] instead.
My personal recommendation is IDM Computing's UltraEdit (www.ultraedit.com) - it can do regular expressions (both search and replace) with Perl, Unix and UltraEdit syntax. Multi-line matching is one of the capabilities in Perl regex mode in it.
It also has other nice search capabilities (e.g search in specific character column range, search in multiple files, search history, search favorites, etc...)
(source: ultraedit.com)
The Zeus editor can do multi-line search and replace.
I use Eclipse, which is free and that you may already have if you are a developer. '\R' acts as platform independent line delimiter. Here is an example of multi-line search:
search:
\bibitem.(\R.)?\R?{([^{])}$\R^([^\].[^}]$\R.$\R.)
and replace:
\defcitealias{$2}{$3}
I'm pretty sure Notepad++ can do that now via the TextFX plugin (which is included by default). Hit Control-R in Notepad++ and have a play.
TextPad has good Regex search and replace capabilities; I've used it for a while and am pretty happy with it.
From the Features:
Powerful search/replace engine using
UNIX-style regular expressions, with
the power of editor macros. Sets of
files in a directory tree can be
searched, and text can be replaced in
all open documents at once.
For more options than you could possibly need, check out "Notepad++ Alternatives" at AlternativeTo.net.
you can use Python Script plugin for Multiline Regular Expression search and replace!
- http://npppythonscript.sourceforge.net/docs/latest/scintilla.html?highlight=pymlreplace#Editor.pymlreplace
# This example replaces any <br/> that is followed by another on the next line (with optional spaces in between), with a single one
editor.pymlreplace(r"<br/>\s*\r\n\s*<br/>", "<br/>\r\n")
I use Notepad++ all the time but it's Regex has alway been a bit lacking.
Sublime Text is what you want.
EditPlus does a good job at search/replace using regex (including multiline)
You could use Visual Studio. Download Express for free if you don't have a copy.
VS's regex is non-standard, so you'd have to use \n:b+[\r\n] instead.
The latest version of UltraEdit has multiline find and replace w/ regex support.
Or if you're OK with using a more specialized regular expression tool for this, there's Regex Hero. It has the side benefit of being able to do everything on the fly. In other words, you don't have to click a button to test your regular expression because it's automatically tested after every keypress.
Personally, I'd use UltraEdit if I'm looking to replace text in multiple files. That way I can just select the files to replace as a batch and click Replace. But if I'm working with a single text file and I'm in need of writing a more complex regular expression then I'd paste it into Regex Hero and work with it there. That's because Regex Hero can save time when you see everything happen in real-time.
ED for windows has two versions of regex, three sorts of cut and paste (selection, lines or blocks, AND you can shift from one to the next (unlike ultra edit, which is clunky at best) with just a mouse click while you are highlighting -- no need to pull down a menu. The sheer speed of getting the job done is incredible, like reading on a Kindle, you don't have to think about it.
You can use a recent version of Notepad++ (Mine is 6.2.2).
No need to use the option ". match newline" as suggested in another answer. Instead, use the adequate regular expression with ^ for "begin of line" and $ for "end of line". Then use \r\n after the $ for a "new line" in a dos file (or just \n in a unix file as the carriage return is mainly used for dos/windows text file):
Ex.: to remove all lines starting with tags OBJE after a line starting with a tag UID (from a gedcom file - used in genealogy), I did use the following search regex:
^UID (.*)$\r\n^(OBJE (.*)$\r\n)+
And the following replace value:
UID \1\r\n
This is matching lines like this:
UID 4FBB852FB485B2A64DE276675D57A1BA
OBJE #M4#
OBJE #M3#
OBJE #M2#
OBJE #M1#
and the output of the replacement is
UID 4FBB852FB485B2A64DE276675D57A1BA
550 instances have been replaced in less than 1 sec. Notepad++ is really efficient!
Otherwise, to validate a Regular expression I like to use the .Net RegEx Tester (http://regexhero.net/tester/). It's really great to write and test on the fly a Reg Ex...
PS.: Also, you can use [\s\S] in your regex to match any character including new lines. So, if you look for any block of "multi-line" text starting with "xxx" and ending with "abc", the following Regex will be fine:^xxx[\s\S]*?abc$ where "*?" is to match as less as possible between xxx and abc !!!