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%]".
Related
I'm trying to clean a CSV file which has a column with contents like this:
Sometexthere1", "code"=>"47.51-2-01"}]
And I would like to remove everything before the first quote (") in order to keep just this:
Sometexthere1
I know that I can use $` to get everything before some match in regex, but I am not understanding how to keep just the string before the first double quote.
Parameter expansion does this well enough:
# Define a variable
s='Sometexthere1", "code"=>"47.51-2-01"}]'
# expand it, removing the longest possible match (from the end) for '"'*
result=${s%%'"'*}
# demonstrate that result by printing it
printf '%s\n' "$result"
...properly returns Sometexthere1.
You probably mean "delete everything after a double quote"? In Open Refine, you can use this GREL formula :
value.replace(/".+/, "")
> Result : Sometexthere1
I have a .csv files where many rows have one of the field values like this:
scl[0]
scl[1]
scl[2]
sda[1]
sda[2]
sda[3]
I am storing them in a variable while reading the csv files in line by line format,like:
set string [$m get cell 0 1]
Now when I do regexp to check whether the cell has scl[0] I am unable to pass the square bracket to this regular expression:
I gave this syntax:
if{[regexp "scl\[0\]" $string]} {
...
}
But the if condition doesn't get executed.
If in case of scl(0), i.e () instead of {} in csv file, I gave {[regexp "scl\[(\]0\[)\]" $string]} which worked. The same format I tried apply to square brackets still it doesn't get evaluated.
Am I missing something?
Please help.
Thanks
Note that \ has special meaning inside double quotes. So just do:
regexp "scl\\[0\\]" $string
or:
regexp {scl\[0\]} $string
You could also use string equal: then you only need to worry about one level of quoting:
string equal {scl[0]} $string
Documentation:
string
In data frame df column c1 has negative numerical values formatted like this:
(1,000,000)
I would like to remove the parentheses from the negative values in df$c1, so as to return:
-1,000,000
I am using the following command in R: df$c1<-gsub('^\\($','-',gsub(',','',df$c1))
But the output is not returning the desired effect.
How can I adjust the regular expression in this R command to return the proper formatting?
gsub("\\((.+)\\)", "-\\1", "(1,000,000)")
# [1] "-1,000,000"
Wouldn't it instead be:
df$c1<-sub('^\\(', '-' , sub('\\)$','',df$c1))
This removes leading left-parens, replacing them with minus signs, and removes trailing right-parens. Your version was insisting that the 'outer' pattern be exactly (, which I doubt would match any items, and was removing commas using the 'inner' call. I changed to sub since there was only the desire to do this once for each element.
I'm trying to write an extremely simple piece of code and tcl is not cooperating. I can only imagine there is a very simple error I am missing in my code but I have absolutely no idea what it could be please help I'm so frustrated!!
My code is the following ...
proc substitution {stringToSub} {
set afterSub $stringToSub
regsub {^.*?/projects} "$stringToSub" "//Path/projects" afterSub
regsub {C:/projects} "$stringToSub" "//Path/projects" afterSub
return $afterSub
}
puts "[substitution /projects] "
puts "[substitution C:/projects] "
The substitution works fine for the second expression but not the first one. Why is that??
I have also tried using
regsub {^/projects} "$stringToSub" "//Path/projects" afterSub
and
regsub {/projects} "$stringToSub" "//Path/projects" afterSub
but neither are working. What is going on??
Since yours two regsub calls don't change the input string (i.e.: $stringToSub) but put the result in the string $afterSub which is returned by the function. You will always obtain the result of the last regsub call and the result of the first regsub call in $aftersub is always overwritten.
Note that the first pattern is more general and include all the strings matched by the second (assuming that $stringToSub is always a path). If you hope to obtain "//Path/projects" for your sample strings, you can simply remove the second regsub call:
proc substitution {stringToSub} {
set afterSub $stringToSub
regsub {^.*?/projects} "$stringToSub" "//Path/projects" afterSub
return $afterSub
}
The first two lines in your procedure will effectively do nothing, since regsub always overwrites the destination variable (afterSub) even when there's 0 matches/substitutions made. From the regsub manual:
This command matches the regular expression exp against string, and either copies string to the variable whose name is given by varName or returns string if varName is not present. (Regular expression matching is described in the re_syntax reference page.) If there is a match, then while copying string to varName (or to the result of this command if varName is not present) the portion of string that matched exp is replaced with subSpec.
There's no need to match C:/projects specifically, because ^.*?/projects will match that text?
The issue is that your second use of the regsub operation is overwriting the substituted value from the first regsub use.
We could simplify the code to just this:
proc substitution {stringToSub} {
return [regsub {^.*?/projects} $stringToSub "//Path/projects"]
}
I have a piece of lua code (executing in Corona):
local loginstr = "emailAddress={email} password={password}"
print(loginstr:gsub( "{email}", "tester#test.com" ))
This code generates the error:
invalid capture index
While I now know it is because of the curly braces not being specified appropriately in the gsub pattern, I don't know how to fix it.
How should I form the gsub pattern so that I can replace the placeholder string with the email address value?
I've looked around on all the lua-oriented sites I can find but most of the documentation seems to revolve around unassociated situations.
As I've suggested in the comments above, when the e-mail is encoded as a URL parameter, the %40 used to encode the '#' character will be used as a capture index. Since the search pattern doesn't have any captures (let alone 40 of them), this will cause a problem.
There are two possible solutions: you can either decode the encoded string, or encode your replacement string to escape the '%' character in it. Depending on what you are going to do with the end result, you may need to do both.
the following routine (I picked up from here - not tested) can decode an encoded string:
function url_decode(str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)",
function(h) return string.char(tonumber(h,16)) end)
str = string.gsub (str, "\r\n", "\n")
return str
end
For escaping the % character in string str, you can use:
str:gsub("%%", "%%%%")
The '%' character is escaped as '%%', and it needs to be ascaped on both the search pattern and the replace pattern (hence the amount of % characters in the replace).
Are you sure your problem isn't that you're trying to gsub on loginurl rather than loginstr?
Your code gives me this error (see http://ideone.com/wwiZk):
lua: prog.lua:2: attempt to index global 'loginurl' (a nil value)
and that sounds similar to what you're seeing. Just fixing it to use the right variable:
print(loginstr:gsub( "{email}", "tester#test.com" ))
says (see http://ideone.com/mMj0N):
emailAddress=tester#test.com password={password}
as desired.
I had this in value part so You need to escape value with: value:gsub("%%", "%%%%").
Example of replacing "some value" in json:
local resultJSON = json:gsub(, "\"SOME_VALUE\"", value:gsub("%%", "%%%%"))