Need help using RegEx to Replace lines - regex

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\((.*)\)\)

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 in search & replace: avoid fixed length of lookaround

In a long corpus of text, I want to make some corrections in certain
environments. However, I am encountering problems when using regex with text
editors. I switched to gedit to have an editor which supports regex in
search & replace.
Crucially, I only want to make changes if the line starts with a certain
pattern (\nm or \mb). The problem is that the element that I want to
replace (o' -> o'o) is not at a fixed length from the beginning of the line
and I can't include the regex in the lookbehind (the lookbehind fails).
Is there any way to include what I am looking for in a simple text editor
regex? Or is this already a step where I have to learn how to script in, for
example, Python?
This is what the regex looks like so far.
(?<=\\(nm|mb)).*o'(?=(q|w|r|t|z|p|s|d|f|g|h|j|k|l|x|c|v|b|n|m|a|i|u|e))
Of course, I can't apply .* in the replace without losing its content.
Put a capture group around .* and a back-reference in the replacement.
Find: (?<=\\(nm|mb))(.*)o'(?=(q|w|r|t|z|p|s|d|f|g|h|j|k|l|x|c|v|b|n|m|a|i|u|e))
Replace: \1o'o

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

Notepad++ Find/Replace Regex Help

I am having issues doing a string replacement in Notepad++, and need some help.
My file:
LastName,(tab)FirstName[optional]MiddleName
Some times there is data that has a middle name, sometimes not.
Public,JohnQ.
Doe,John
Clinton,WilliamJefferson
would be:
Public(tab)John(tab)Q
Doe(tab)John
Clinton(tab)William(tab)Jefferson
I want to split it out into this:
LastName(tab)FirstName(tab)MiddleName
Thanks for adding the sample input. It helps immensely to have that around. Try this and see if it does what you want.
Find, making sure Match case is checked:
([A-Z][a-z]*),([A-Z][a-z]*)(.*)
Replace with:
\1(tab)\2(tab)\3
Of course, (tab) is actually a tab character that you have to place in the replacement string yourself.
An ugly regex like this works for me on the example you've provided:
(\w+),(\w+?)(([A-Z]\w*\.?)?)\n
replace with
\1\t\2\t\3\n
Note:
This only works if the middle name starts with a letter in the A-Z. You might be able to replace [A-Z] with [[:upper:]] if notepad++ supports it (I don't know).
I need that second bracket around the middle name part because I need to match at least an empty string when there is no middle name.