I am trying to modify my URL to be clean and friendly by removing more than one occurrence of specific characters
local function fix_url(str)
return str:gsub("[+/=]", {["+"] = "+", ["/"] = "/", ["="] = "="}) --Needs some regex to remove multiple occurances of characters
end
url = "///index.php????page====about&&&lol===you"
output = fix_url(url)
What I would like to achieve the output as is this :
"/index.php?page=about&lol=you"
But instead my output is this :
"///index.php????page====about&&&lol===you"
Is gsub the way i should be doing this ?
I don't see how to do this with one call to gsub. The code below does this by calling gsub once for each character:
url = "///index.php????page====about&&&lol===you"
function fix_url(s,C)
for c in C:gmatch(".") do
s=s:gsub(c.."+",c)
end
return s
end
print(fix_url(url,"+/=&?"))
Here's one possible solution (replace %p with whatever character class you like):
local
function fold(s)
local ans = ''
for s in s:gmatch '.' do
if s ~= ans:sub(-1) then ans = ans .. s end
end
return ans
end
local
function fix_url(s)
return s:gsub('%p+',fold) --remove multiple same characters
end
url = '///index.php????page====about&&&lol===you'
output = fix_url(url)
print(output)
Related
I want to write a method that takes a string as an argument and returns the same sentence with each word reversed in place.
example:-
reverse_each_word("Hello there, and how are you?")
#=> "olleH ,ereht dna woh era ?uoy"
I tried:
def reverse_each_word(string)
array = []
new_array=array <<string.split(" ")
array.collect {|word| word.join(" ").reverse}
end
but the return is
["?uoy era woh dna ,ereht olleH"]
The whole sentence is reversed and not sure why there is [ ]
any help??
As an alternative approach, you could use gsub to find all words and reverse them:
str = "Hello there, and how are you?"
str.gsub(/\S+/, &:reverse)
#=> "olleH ,ereht dna woh era ?uoy"
/\S+/ matches one or more (+) non-whitespace (\S) characters
def reverse_each_word(string)
new_array = string.split(" ")
new_array.collect {|word| word.reverse }.join(" ")
end
So I am having a issue with Lua gsub removing apostrophes from strings if there is one apostrophe on its own or loads of them i can't seem to get it to remove any of them.
local uri_without_args = "'" --one on its own
local uri_without_args = "''''''''lol''''" --in text
local uri_without_args = "''''''''''''" --loads
--etc--etc all occurrences must go
local list = {
"%'", --apostrophe
}
for k,v in ipairs(list) do
local uri_without_args_remove_duplicates, occurrences = uri_without_args:gsub(""..v.."","")
if occurrences > 0 then
occurrences = occurrences + 1
for i=1, occurrences do
if uri_without_args_remove_duplicates=="" then
--do nothing
else
uri_without_args = uri_without_args:gsub(""..v.."","")
end
end
end
end
print(uri_without_args)
The only time you assign a new value to uri_without_args is when uri_without_args_remove_duplicates is not empty. If you either remove the if statement from around the assignment to uri_without_args, or if uri_without_args starts off as "''''''''lol''''", then it works fine.
As Egor said in the comment, you also could simply use uri_without_args_remove_duplicates as the result value.
Using LUA how would I convert a table such as this to all lowercase?
return {{[ [[House]] ] = [[bob]],[ [[Roof]] ] = [[steve]],[ [[Door-Knob]] ] = [[Richard]],[ [[Rug-Duty]] ] = [[mark]],},
Assuming your input table is of the following format (I've purposefully mixed upper and lower case characters in it):
test_tbl = {["HOUse"]="BOB", ["Roof"]="STEVE"}
you can write a function as follows:
function convert_table(tbl_test)
local output_table = {}
for i, v in pairs(tbl_test) do
output_table [string.lower(i)] = string.lower(v)
end
return output_table
end
and after calling it: local returned_table= convert_table(test_tbl) you can verify that all keys and values are in lowercase:
for i,v in pairs(returned_table) do
print(i,v)
end
I am trying to make a lua script that takes an input of numbers seperated by commas, and turns them into letters, so 1 = a ect, however I have not found a way to do this easily because the string libray outputs a = 97, so I have no clue where to go now, any help?
You can use string.byte and string.char functions:
string.char(97) == "a"
string.byte("a") == 97
If you want to start from "a" (97), then just subtract that number:
local function ord(char)
return string.byte(char)-string.byte("a")+1
end
This will return 1 for "a", 2 for "b" and so on. You can make it handle "A", "B" and others in a similar way.
If you need number-to-char, then something like this may work:
local function char(num)
return string.char(string.byte("a")+num-1)
end
Merely just account for the starting value of a-z in the ascii table.
function convert(...)
local ar = {...}
local con = {}
for i,v in pairs(ar) do
table.insert(con, ("").char(v+96))
end
return con;
end
for i,v in pairs(convert(1,2,3,4)) do
print(v)
end
Alternatively to these answers, you could store each letter in a table and simply index the table:
local letters = {'a','b','c'} --Finish
print(letters[1], letters[2], letters[3])
Define your encoding as follows:
encoding = [[abc...]]
in whatever order you want.
Then use it as follows
function char(i)
return encoding:sub(i,i)
end
If the list of numbers is in a table, then you may use
function decode(t)
for i=1,#t do t[i]=char(t[i]) end
return table.concat(t)
end
You can also save the decoding in a table:
char = {}
for i=1,#encoding do char[i]=encoding:sub(i,i) end
and use char[t[i]] in decode.
I've been using the following code for the problem. I'm making a program to change the IUPAC name into structure, so i want to analyse the string entered by the user.In IUPAC name there are brackets as well. I want to extract the compound name as per the brackets. The way I have shown in the end.
I want to modify the way such that the output comes out to be like this and to be stored in an array :
As ["(4'-cyanobiphenyl-4-yl)","5-[(4'-cyanobiphenyl-4-yl)oxy]",
"({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}" .... and so on ]
And the code for splitting which i wrote is:
Reg_bracket=/([^(){}\[\]]*)([(){}\[\]])/
attr_reader :obrk, :cbrk
def count_level_br
#xbrk=0
#cbrk=0
if #temp1
#obrk+=1 if #temp1[1]=="(" || #temp1[1]=="[" ||#temp1[1]=="{"
#obrk-=1 if #temp1[1]==")" || #temp1[1]=="]" ||#temp1[1]=="}"
end
puts #obrk.to_s
end
def split_at_bracket(str=nil) #to split the brackets according to Regex
if str a=str
else a=self
end
a=~Reg_bracket
if $& #temp1=[$1,$2,$']
end
#temp1||=[a,"",""]
end
def find_block
#obrk=0 , r=""
#temp1||=["",""]
split_at_bracket
r<<#temp1[0]<<#temp1[1]
count_level_br
while #obrk!=0
split_at_bracket(#temp1[2])
r<<#temp1[0]<<#temp1[1]
count_level_br
puts r.to_s
if #obrk==0
puts "Level 0 has reached"
#puts "Close brackets are #{#cbrk}"
return r
end
end #end
end
end #class end'
I ve used the regex to match the brackets. And then when it finds any bracket it gives the result of before match, after match and second after match and then keeps on doing it until it reaches to the end.
The output which I m getting right now is this.
1
2
1-[(
3
1-[({
4
1-[({5-[
5
1-[({5-[(
4
1-[({5-[(4'-cyanobiphenyl-4-yl)
3
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]
2
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}
1
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)
0
1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]
Level 0 has reached
testing ends'
I have written a simple program to match the string using three different regular expressions. The first one will help separate out the parenthesis, the second will separate out the square brackets and the third will give the curly braces. Here is the following code. I hope you will be able to use it in your program effectively.
reg1 = /(\([a-z0-9\'\-\[\]\{\}]+.+\))/ # for parenthesis
reg2 = /(\[[a-z0-9\'\-\(\)\{\}]+.+\])/ # for square brackets
reg3 = /(\{[a-z0-9\'\-\(\)\[\]]+.+\})/ # for curly braces
a = Array.new
s = gets.chomp
x = reg1.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg1.match(str)
a << x.to_s
str = x.to_s.chop
end
x = reg2.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg2.match(str)
a << x.to_s
str = x.to_s.chop
end
x = reg3.match(s)
a << x.to_s
str = x.to_s.chop.reverse.chop.reverse
while x != nil do
x = reg3.match(str)
a << x.to_s
str = x.to_s.chop
end
puts a
The output is a follows :
ruby reg_yo.rb
4,4'{-1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)oxy]ethylene}dihexanoic acid # input string
({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)
(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)
(4'-cyanobiphenyl-4-yl)
[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)oxy]
[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]
[(4'-cyanobiphenyl-4-yl)oxy]
{-1-[({5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}oxy)carbonyl]-2-[(4'-cyanobiphenyl-4-yl)oxy]ethylene}
{5-[(4'-cyanobiphenyl-4-yl)oxy]pentyl}
Update : I have modified the code so as to search for recursive patterns.