Scite Lua - escaping right bracket in regex? - regex

Bumped into a somewhat weird problem... I want to turn the string:
a\left(b_{d}\right)
into
a \left( b_{d} \right)
in Scite using a Lua script.
So, I made the following Lua script for Scite:
function SpaceTexEquations()
editor:BeginUndoAction()
local sel = editor:GetSelText()
local cln3 = string.gsub(sel, "\\left(", " \\left( ")
local cln4 = string.gsub(cln3, "\\right)", " \\right) ")
editor:ReplaceSel(cln4)
editor:EndUndoAction()
end
The cln3 line works fine, however, cln4 crashes with:
/home/user/sciteLuaFunctions.lua:49: invalid pattern capture
>Lua: error occurred while processing command
I think this is because bracket characters () are reserved characters in Lua; but then, how come the cln3 line works without escaping? By the way I also tried:
-- using backslash \ as escape char:
local cln4 = string.gsub(cln3, "\\right\)", " \\right) ") -- crashes all the same
-- using percentage sign % as escape chare
local cln4 = string.gsub(cln3, "\\right%)", " \\right) ") -- does not crash, but does not match either
Could anyone tell me what would be the correct way to do this?
Thanks,
Cheers!

The correct escape character in Lua is %, so what you tried should work, I just tried
local sel = [[a\left(b_{d}\right)]]
local cln3 = string.gsub(sel, "\\left%(", " \\left( ")
local cln4 = string.gsub(cln3, "\\right%)", " \\right) ")
print (cln4)
and got
a \left( b_{d} \right)
so, this worked for me when I tried it, what did you get as a match when you tried %

Related

Replace including a word with an initial parenthesis

Hving trouble with this msaccess query:
Replace([OurClient]," dba"," DBA"," ERRONEOUSLY "," erroneously ","(SUED ","(sued ")
All of these work except
(SUED
is not being replaced with
sued
expected (sued
but still got (SUED

Replace Line Break VTL

I need to replace all line breaks with a ^ character using VTL. I unfortunately do not know which version of VTL I am using as it is used as the formula language within an application. Please see below with what we have so far:
#if($FIELD} !="")
#set($new = "
")
${$FIELD}.replaceAll("$new", "^" )
#end
Try plugging in the Hex code for Line Feed(0A)"
#if($FIELD.contains(//0A)) #set($FIELD2=$FIELD.replaceAll("//0A", "^")) $FIELD2

Regex to find 4th value inside bracket

How i can read 4th Value(inside "" i.e "vV0...." using Regex in below condition ?
I am updating a bit this part - Is it possible to first find Word "LaunchFileUploader" and then select the 4th Value, if there are multiple instance of LaunchFileUploader in the file just select 4th Value of first word found ? Attaching screenshot of file where this needs to be searched (In the file word is "LaunchFileUploader")
I tried this but it gives as - I need 4th value (Group 1 is giving me third value)
\bLaunchFileUploader\b(\:?.*?,){3}.*?\)
Match 1
Full match 11030-11428 LaunchFileUploader("ERM-1BLX3D04R10-0001", 1662, "2ecbb644-34fa-4919-9809-a5ff47594c2d", "8dZOPyHKBK...
Group 1. n/a "2ecbb644-34fa-4919-9809-a5ff47594c2d",
I am still looking for solution for this. Any help is aprreciated.
Depending on what's available to you to use, there's a couple of ways to do it.
Either way, this would work better if there were no new lines in the string, just plain ("value1","value2","value3","value4") etc. It'll still work, but you may need to clean up some new lines from the resulting string.
The easy way - use code for the hard part. Grab the inner string with:
(?<=\().*?(?=\))
This will get everything that's between the 2 parentheses (using positive lookarounds). In code, you could then split/explode this string on , and take the 4th item.
If you want to do it all in regex, you could use something along the lines of:
(?<=\()(?:.*?,){3}(.*?)(?=\))
This would a) match the entire contents of the parentheses and b) capture the 4th option in a capture group. To go even deeper:
(?<=\()(?:.*?,){3}\"(.*?)\"(?=\))
would capture the contents of the "" quotation marks only.
Some tools don't allow you to use lookarounds, if this is the case let me know and I'll see what other ways there are around it.
EDIT Ran this in JS console on browser. This absolutely does work.
EDIT 2 I see you've updated your question with the text you're actually searching in. This pattern will include the space and the new line character as per the copy/paste of the above text.
(?<=\(\")(?:.*?,\s?\n?){3}\"(.*?)\"(?=\))
See my second image for the test in console
This works for python and PHP:
(?<=\")(.*)(?:\"\);)\Z
Demo for Python and PHP
For Java, replace \Z with $ as follows:
(?:")(.*)(?:\"\);)$
Demo for JavaScript
NOTE: Be sure to look the captured group and not the matched group.
UPDATE:
Try this for your updated request:
"(.*)"(?:[\\);\] \/>}]*)$
Demo for updated input string
all the above regex patterns assume there is a line break after each comma
Auto-generated Java Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "\"(.*)\"(?:[\\\\);\\] \\/>\\}]*)$";
final String string = "\n"
+ "}$(document).ready( function(){ PathUploader\n"
+ " (\"ERM-1BLX3D04R10-0001\", \n"
+ " 1662, \n"
+ " \"1bff5c85-7a52-4cc5-86ef-a4ccbf14c5d5\", \n"
+ "\"vV0mX3VadCSPnN8FsAO7%2fysNbP5b3SnaWWHQETFy7ORSoz9QUQUwK7jqvCEr%2f8UnHkNNVLkJedu5l%2bA%2bne%2fD%2b2F5EWVlGox95BYDhl6EEkVAVFmMlRThh1sPzPU5LLylSsR9T7TAODjtaJ2wslruS5nW1A7%2fnLB%2bljZaQhaT9vZLcFkDqLjouf9vu08K9Gmiu6neRVSaISP3cEVAmSz5kxxhV2oiEF9Y0i6Y5%2f5ASaRiW21w3054SmRF0rq3IwZzBvLx0%2fAk1m6B0gs3841b%2fw%3d%3d\"); } );//]]>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}

Capturing preceding whitespace in Julia

I have a very long piece of code that I need to add to, and would prefer to do it using a script rather than write myself for fear of introducing errors.
I have commands that look like this
rename oldname newname
rename oldname2 newname2
I want to, whenever I see the command "rename" I want to add a "note" command
rename oldname newname
note newname: "A Note"
rename oldname2 newname2
note newname2: "A Note"
I am using Julia's read and write features to do this, and it has been very easy so far.
f = open("renaming.txt") # open input file
g = open("renaming_output.txt", "w") # open output file
for ln in eachline(f)
write(g, "$ln \n") # write the command, no matter what it is
stripped = lstrip("$ln") # Strip whitespace so I can use "startswith" command
if startswith(stripped, "ren")
words = split("$ln", " ") # split on space to get the "newvar" name
println("$ln \n") #check that I am working with a rename command
println("note ", words[3]":") # check that the note function prints
note_command = string("note ", words[3], ": \n") # construct the note command
write(g, note_command) #write it to the output file.
end
end
My issue is with the indentation. The above code writes the "note" command on the far left, without any indentation. However, Ideally I would like the note command to be indented one level further than the rename command. But I can't figure out how to capture all the preceeding whitespace.
I presume that the answer involves using the match and m.match functions, but I can't get it to work.
Any help would be appreciated.
On Julia 0.7 the simplest change in your code would be to replace
println("note ", words[3]":")
with
println(first(ls, search(ls, 'r')-1), " note ", words[3]":")
Using regular expressions you can write rx = r"(?:(?!r).)*" at the start of your code and then:
println(match(rx, ls).match, " note ", words[3]":")
In both cases we take care to retain the start of ls till 'r' in its original form.
With Julia 6.1, my solution, with the help the answer, is as follows
if startswith(stripped, "ren") & !startswith(stripped, "renvars")
leftpad = " " ^ search("$ln", 'r')
words = split(stripped, " ")
varname = string(leftpad, " note ", words[3], ": ", words[2], " \n")
print(varname)
write(g, varname)
end
With the leftpad = ^ search("$ln", 'r') being the key addition. Given that the left padding of my code is always tabs, I just insert the number of tabs as there are characters before the first r. This solution works in 0.6.1, but search may behave differently in .7.

What are Lua's string.gsub pattern rules?

Here is the current script that have running:
var_name="[Clan] Imposter"
while var_name:find("[Clan]")~=nil do
var_name=var_name:gsub("[Clan]", "")
end
print(var_name)
I was expecting var_name to be "Imposter" however the result was "[] Imposter".
How do I get the result from this function to be "Imposter"?
You need to escape square brackets in patterns: "%[Clan%]".