Regex Command to remove function from all files - regex

I am trying to remove the following function from all my working files, while leaving the first argument intact. The second argument changes every time.
dotranslate( "Arg1", "Arg2" )
I am trying to do this using Notepad++, but I just can't seem to get it right.

If your strings can contain escaped quotes, this will be quite difficult. If not, you can go with this:
Find what: dotranslate\(\s*("[^"]*")\s*,\s*"[^"]*"\s*\)
Replace with: $1
So this will match dotranslate(, then optional spaces, then capture a string. The string is written as "[^"]*". So quotes, as many non-quotes as possible and quotes again. And then we just match spaces, comma, spaces, string, spaces, closing parenthesis.
And replace that with what we captured in the first (and only) set of unescaped parentheses. Which is the first string.
If Args1 should not be variable, simply write the specific value into the capturing group.

Related

Regex syntax \(.*\) suppose to remove the ( ) and all characters between ( ). How it actually works?

I am new to Regex world. I would like to rename the files that have time stamp added on the end of the file name. Basically remove last 25 characters before the extension.
Examples of file names to rename:
IMG523314(2021-12-05-14-51-25_UTC).jpg > IMG523314.jpg
Test run1(2021-08-05-11-32-18_UTC).txt > Test run1.txt
To remove 25 characters before the .extension (2021-12-05-14-51-25_UTC)
or if you like, remove the brackets ( ) which are always there and everything inside the brackets.
After the right bracket is always a dot '. "
Will Regex syntax as shown in the Tittle here, select the above? If yes, I wonder how it actually works?
Many Thanks,
Dan
Yes \(.*\) will select the paranthesis and anything inside of them.
Assuming when you ask how it works you mean why do the symbols work how they do, heres a breakdown:
\( & \): Paranthesis are special characters in regex, they signify groups, so in order to match them properly, you need to escape them with backslashes.
.: Periods are wildcard matcher, meaning they match any single character.
*: Asterisks are a quantifier, meaning match zero to inifite number of the previous matcher.
So to put everything together you have:
Match exactly one opening parathesis
Match an unlimited number of any character
Match exactly one closing bracket
Because of that closing bracket requirement, you put a limit to the infinite matching of the asterisk and therefore only grab the parenthesis and characters inside of them.
Yes, it's possible:
a='IMG523314(2021-12-05-14-51-25_UTC).jpg'
echo "${a/\(*\)/}"
and
b='Test run1(2021-08-05-11-32-18_UTC).txt'
echo "${b/\(*\)/}"
Explanation:
the first item is the variable
the second is the content to be replaced \(*\), that is, anything inside paranthesis
the third is the string we intend to replace the former with (it's empty string in this case)

Regex (Notepad++) - Locate and replace second occurance of a character per line

I would like to change the second forward slash, within each line, to a comma.
I have found various posts and managed to derive a way of doing it from them but it's not doing it how I want.
Initial attempt - I thought I needed to replace between 2 delimiters
1st "Replace 2nd occurrence" - Found this post which seemed easier.
2nd "Replace 2nd occurrence"- Used the regex in here as a base for mine.
What I am doing is;
Find:
^(.*?)\/(.*?)\/
Replace:
$&,
Which results in changing my data from;
042146/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:58,
To;
042146/OVERNIGHT/,HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/,HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/,HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/,HSSC825571,ended,14/07/2016,00:00:58,
Is there a way of just replacing the second /?
An example set of my data is;
042146/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:58,
042154/TEMP56/QPADEV000M,started,14/07/2016,00:01:02,V0700LRFIN
042154/TEMP56/QPADEV000M,ended,14/07/2016,00:07:12,
042155/JMALICKA/QPADEV000N,started,14/07/2016,00:01:05,V0700LRFIN
042155/JMALICKA/QPADEV000N,ended,14/07/2016,00:06:53,
042156/DG8SVCPRF/DG8SVC,started,14/07/2016,00:01:15,DATAGATE
042156/DG8SVCPRF/DG8SVC,ended,14/07/2016,00:12:01,
042157/OVERNIGHT/RCPTDISCRP,started,14/07/2016,00:01:42,V0700LBATC
042157/OVERNIGHT/RCPTDISCRP,ended,14/07/2016,00:01:44,
042158/QTCP/QTSMTPCLTP,started,14/07/2016,00:01:53,QSYSWRK
042158/QTCP/QTSMTPCLTP,ended,14/07/2016,01:29:08,
042159/QTCP/QTSMTPCLTP,started,14/07/2016,00:01:53,QSYSWRK
042159/QTCP/QTSMTPCLTP,ended,14/07/2016,00:19:05,
Ctrl+H
Find what: ^([^/]+/[^/]+)/
Replace with: $1,
Replace all
This will replace the second slash of each line by a comma.
You were almost there. You only need to change your replace string with the following:
$1/$2,
How it works
Your regex was: ^(.*?)\/(.*?)\/
In Notepad++'s replace string, the dollar sign is used to refer to groups enclosed by parentheses in the regex.
$1 refers to the first group, (.*?) which is at the beginning of the line, as specified with the ^ character.
$2 refers to the second group, also (.*?), but which follows the first /.
Since you don't want to replace the first slash, you need $1/$2 at the beginning of your replace string. But since what follows the second group is another / (the 2nd one on the line), you need to replace it with the ,. That's why the replace string has to be $1/$2,. Notice that all characters that are not enclosed by ()'s need to be re-written in the replace string. Otherwise, they're just omitted (try replace string $1$2 and you'll see what I mean).
In other editors or programming languages, instead of the $ sign, the \ is sometimes used (sometimes doubled) to refer to parenthetic groups. So you could have for instance \\1/\\2, or \1/\2, as a replace string instead of $1/$2,.

remove all commas between quotes with a vim regex

I've got a CSV file with lines like:
57,13,"Bob, Bill and Susan",Student,Club,Funded,64,3200^M
I need them to look like
57,13,Bob-Bill-and-Susan,Student,Club,Funded,64,3200
I'm using vim regexes. I've broken it down into 4 steps:
Remove ^M and insert newlines:
:%s:<ctrl-V><ctrl-M>:\r:g`
Replace all with -:
:%s: :\-:g
Remove commas between quotes: Need help here.
Remove quotes:
:%s:\"\([^"]*\)\":\1:g
How do I remove commas between quotes, without removing all commas in the file?
Something like this?
:%s:\("\w\+\),\(\w\+"\):\1 \2:g
My preferred solution to this problem (removing commas inside quoted regions) is to use replacements with an expression instead of trying to get this done in one regex.
To do this you need to prepend you replacement with \= to get the replacement treated as a vim expression. From here you can extract just the parts between quotes and then manipulate the the matched part separately. This requires having two short regexes instead of one complicated one.
:%s/".\{-}"/\=substitute(submatch(0), ',', '' , 'g')/g
So ".\{-}" matches anything in quotes (non greedy) and substitute(submatch(0), ',', '' , 'g') takes what was matched and removes all of the commas and its return value is used as the actual replacement.
The relevant help page is :help sub-replace-special.
As for the other parts of your question. Step 1 is essentially trying to remove all carriage returns since the file format is actually the dos file format. You can remove them with the dos2unix program.
In Step 2 escaping the - in the replacement is unnecessary. So the command is just
:%s/ /-/g
In Step 4, you have an overly complicated regex if all you want to do is remove quotes. Since all you need to do is match quotes and remove them
:%s/"//g
:%s:\("\w*\)\(,\)\(.*"\):\1\3:g
example: "this is , an, example"
\("\w*\) match start of " every letter following qoutes group \1 for back reference
\(,\) capture comma group \2 for back reference
(.*"\) match every other character upto the second qoute ->group 3 for backreference
:\1\3: only include groups without comma, discard group 2 from returned string which is \2
:%s:\("\w*\)\(,\)\(.*"\):\1\3:g removes commas

Regex to change all past a certain pattern to Uppercase

I have an xml file that has a value like
JOBNAME="JBDSR14353_Some_other_Descriptor"
I am looking for an expression that will go through the file and change all of the characters in the quotes to Uppercase letters. Is there a Regex expression that will search for JOBNAME="Anything within the quotes" and change them to uppercase? Or a command that will find JOBNAME= and change all on that line to uppercase letters? I know that can just do a search for JOBNAME= and then use a VU command in vim to throw the line to uppercase store that to a macro and run that, but I was wondering if there was a way to get this done with a regex??
Here's an alternative with :substitute, as you had originally intended. This works better than #Zach's solution with gU_ when there's other text in the line:
:%s/JOBNAME="[^"]\+"/\U&/g
"[^"]\+" matches the quoted text (non-greedily by matching only non-quotes inside, to handle multiple quotes in a line)
\U turns the remainder of the replacement uppercase
for simplicity, the entire match (&) is uppercased here, but one could have also used capture groups (\(...\)), or match limiting with \zs
You can use the :g command which executes a command on lines that match a pattern:
:g/JOBNAME/norm! gU_
This will execute the gU_, which capitalizes all letters on a line, on all the lines that match JOBNAME
If there are other things on the same line that you don't want to capitalize, here is a solution for only the words in quotes:
:g/JOBNAME/norm! f"gU;
f" goes to the next quote. gU capitalizes with a motion. The motion used is ; which searches for the next " (repeats the last f command).
To do this with substitution you can use the \U atom which makes everything after it uppercase.
:%s/JOBNAME="\zs.*\ze"/\U&
\zs and \ze mark the start and end of the match and & is the whole match. This means that only the part between quotes is replaced.

Using regex to replace unescaped quotes

I'm trying to use a regex search and replace to find and fix any unescaped quotation marks with escaped question marks. This is not in any particular language - just using regex to search and replace in Sublime Text 2.
I can find them just fine with this regex:
([a-zA-Z0-9!##$%^&*()_+=-\?><:;\/])\"
Trying to replace is giving me some headaches. I thought this would work:
$0\\\"
but it's adding an extra quote in (or leaving the previous one there somehow).
e.g.,
e"
becomes
e"\"
instead of just
e\"
What the hey? I can't seem to find a combination in the replacement that will work!
In the replacement $0 will be a reference to the entire match, including the quote. It looks like you should be using $1 instead which will be the first capturing group, so just the character immediately before the quote. So your replacement string would be "$1\\\"".