I want to know how to get text form a particular part of a sentence this is what i mean. Hello (new text)., in the brackets it says new text and i want to find the text in the brackets and print only the text in the brackets.
With the ( and ) surrounding the text, you can use string.match:
str = "Hello (new text)"
print(str:match("%((.+)%)"))
%((.+)%)") is a pattern that captures every character between the ( and ).
Resources:
Lua 5.3 Reference Manual - 6.4.1 Patterns
The easiest way to do this is just to cancel out the Hello in your string.
local msg = "Hello (obama)"
msg = msg:gsub("Hello %(", ""):gsub("%)", "")
If the first part of the string is dynamic and is not always hello you can try this:
local msg = "Bye (obama)"
local pattern = "%p.+%p"
print(({string.match(msg, pattern):gsub("%)", ""):gsub("%(", "")})[1])
This soloution may not be the best and most efficient but it certainly works. I hope it helped you. Let me know if you have any questions.
Related
So I am making a function in lua where i wan't it to split a string at every "/" so for example:
local s = "Hello/GoodBye/Hi"
then i want it to split and input it to a table so it will look something like this:
Hello
GoodBye
Hi
This is my attempt but it doesn't really work:
local STR = "Hello/GoodBye/Hi"
strings = {}
for q,string in STR:gmatch("([/])(.-)%1") do
table.insert(strings, string)
end
That just returns:
Hello
Hi
That just returns:
Hello
Hi
When I run your code there is only a single element in strings which is "GoodBye".
The easiest way is to match anything that is not a / as in "[^/]+".
Also naming a variable string is not a good idea. You're shadowing Lua`s string library.
Im trying to perform a search in DXL of a string that ends with specific characteres Im not able to find the way to perform this.
Example, I'm looking for
" A: 23.1.23.2.4"
But if this contains at the end the character "~" the find function does not work
Example Where the skip list contains "A: 12.2.1.4.5~ text text text text"
I just need to know in the object.text contains A: 12.2.1.4.5
string string_text = "A: 12.2.1.4.5"
if(find(skip[i],string_text,string_text)){
modify_attributes(req_text)
}else{
output << "stgring not found : "
}
use a regular expression, like this
void modify_attributes (string fulltext) {print "modifying.."}
string fulltext = "A: 12.2.1.4.5~ text text text text"
Regexp searchme = regexp2 "A: 12.2.1.4.5"
if(searchme (fulltext)){
modify_attributes(fulltext)
}else{
print "string not found "
}
The "find"-method for Skip lists is O(1), if I am not mistaken. But for that to work properly, the key, you are asking for, has to match exactly.
So, to benefit from the speed of value-retrieval by the find method, I suggest, that you have a look at your code part, where you put stuff into your Skip, (only put "clean" information in the Skip, which you know, you want to ask for later on).
That of course only works, if you have the possibility to do so, i.e. you don't get the Skip from somewhere you don't have control over..
I am working on a project, in which you type your input sentence, and I need to be able to use " and ' in the sentence, such as Input = "I said, "Hi what's up?" print(Input) in which I get an error. If anyone knows how to fix this that would be great.
See https://www.lua.org/pil/2.4.html. Lua has very interesting feature to declare string with square brackets:
input = [[I said, "Hi what's up?"]]
input = "I said, \"Hi what's up?\""
input = 'I said, "Hi what\'s up?"'
I will tell some things in addition to what #Darius told above
When you tried to add a quatation mark inside a string, the lua interpreter get confused and break your string after the next quation mark without reaching the end of the line. That's the reason for the error.
Try to understand it by the following code
str = "Hello I"m somebody" -- here the interpreter will think str equals to "Hello I" at first, and then it will find some random characters after which may make it confused (as m somebody is neither a variable nor a keyword)"
-- you can also see the way it got confused by looking at the highlighted code
--What you can do to avoid this is escaping the quotes
str = "Hello I\"m somebody" -- here the interpreter will treat \" as a raw character (") and parse the rest.
You can also use the escape character () with others such as \', \", \[, \n (newline character), \t (tab) and so on.
I am working on string manipulation using LUA and having trouble with the following problem.
Using this as an example of the original data I am given -
"[0;1;36m(Web): You say, "Text here."[0;37m"
I want to keep the string intact except for removing the ANSI codes.
I have been pointed toward using gsub with the LUA pattern matching but I cannot seem to get the pattern correct. I am also unsure how to reference exactly the escape character sent.
text:gsub("[\27\[([\d\;]+)m]", "")
or
text:gsub("%x%[[%d+;+]m", "")
If successful, all I want to be left with, using the above example, would be:
(Web): You say, "Text here."
Your string example is missing the escape character, ASCII 27.
Here's one way:
s = '\x1b[0;1;36m(Web): You say, "Text here."\x1b[0;37m'
s = s:gsub('\x1b%[%d+;%d+;%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+m','')
:gsub('\x1b%[%d+m','')
print(s)
I'm fairly new to this forum. I am having trouble with manipulating the correct string to achieve this.
Basically, what I'm trying to do is receive an input string like this example:
str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in pairs(string.gmatch(s, "(%w+)=(%w+)"))
print(question, answer)
end
I want it to return: question = "Say hello to" and answer = "Stack overflow, question = "Say goodbye to" and so on and so forth. but instead, it picks up the word just before the equal sign and the word just after. I've even tried the * quantifier, and it does the same exact thing.
I've also tried this pattern
[%w%s]*=[%w%s]
I just want to be able to sort this string into a key-value table where the key is all words before each = and the value is all words after that equal but before the comma.
Does anyone have a suggestion?
You can use something like this:
local str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in string.gmatch(str..",", "([^=]+)=([^,]+),%s*") do
print(question, answer)
end
"([^=]+)=([^,]+),%s*" means the following: anything except = ([^=]) repeated 1 or more times (+) followed by = and then anything except ',', followed by comma and optional whitespaces (to avoid including them in the next question). I also added comma to the string, so it parses the last pair as well.
To elaborate a bit further per request in the comments: in the expression [^=]+, [=] designates a set with one allowed character (=) and [^=] negates that, so it's a set with any character allowed except = and + allows the set to be repeated 1 or more times.
As #lhf suggested you can use a simpler expression: (.-)=(.-),%s*, which means: take all characters until the first = (- makes matching non-greedy) and then take all characters until the first ,.