Lua pattern to remove everything after a word - lua

If I have a string like this.
local string = "D:/Test/Stuff/Server/resources/[Test]/SuperTest"
How would I remove everything after the word "server" so it will end up look like this
local string = "D:/Test/Stuff/Server"

use str instead of string to not shadow the real string library
local str = "D:/Test/Stuff/Server/resources/[Test]/SuperTest"
here a solution
str = str:match("(.*Server)")
or use do...end to avoid shadowing
do
local string = ("D:/Test/Stuff/Server/resources/[Test]/SuperTest"):match("(.*Server)")
end

Related

How to convert string into list string on specific expression in Dart?

i have string who is
String hello = "test test {Brian} have you {Adam} always and {Corner} always";
want to make list string who is taking who have {string}
output :
List<String> data = ["{Brian}","{Adam}","{Corner}"];
its that possible in dart ?
i dont know what to use
You can achieve this by using RegExp(r"\{[^{}]*\}")
String hello = "test test {Brian} have you {Adam} always and {Corner} always";
RegExp regExp = RegExp(r"\{[^{}]*\}");
print(regExp.allMatches(hello).map((e) => e[0]).toList());

How to remove last element from a list in dart?

I'm a beginner in dart.
void main() {
var abf = '+37.4054-122.0999/';
var abf2;
abf2 = abf.replaceAll("+"," ");
var abf1 = abf2.split(RegExp('(?=[+-])'));
print (abf1[0]);
print (abf1[1]);
}
The above code splits abf into two values for me
I want to remove the ending '/'. I tried many split methods using other variables but it's not removing the '/' even though its removing the '+'.
It's not really clear what you're trying to do with the split.
But if you're looking the remove the / this should work:
String number = '+37.4054-122.0999/';
number = number.replaceAll("/"," ");
You can create substring from this while you like to remove last element.
String abf = '+37.4054-122.0999/';
final result = abf.substring(0, abf.length - 1);
print(result);
Dart's List class has a built-in removeLast method. Maybe you can try to split the string and then removing the last element:
String str = "str";
String newStr = str.split(''). removeLast().join('');

Ruby string not longer than n number of characters

This is probably something very simple, but is there a way to make a string shorter. Something like:
string = "1234567980987654321";
From this string above I want only the first 9 characters.
new_string = "123456789"
You can use functionality built into the String class.
string = "1234567980987654321"
new_string = string[0, 9] #=> "123456798"
To add on for Rails (since it was tagged for Rails too),
You can use String#first
string = "1234567980987654321"
string.first(9) #=> "123456798"
In Ruby or Rails, another way do achieve your result would be by using range:
string = "1234567980987654321"
new_string = string[0..8] #=> "123456798"

Lua string concatenation in Json blob

I have a question about string concatenation.
I have this example where I am trying to append a value inside a json block with a variable value
example:
clock = os.clock()
body = "{\"name\":\"stringValue\" .. clock }"
print(body)
When I run this I get the following output:
{"name":"stringValue" .. clock }
What I am expecting is something like
{"name":"stringValue0.010117"}
How do I make is so this variables value is added to the string?
This is an instance were using [[ ]] delimited strings is useful:
clock = os.clock()
body = [[{"name":"stringValue]] .. clock .. [["}]]
print(body)
To continue using a double quoted string, your variable assignment would look like the following (note how the quote after stringValue is not escaped):
body = "{\"name\":\"stringValue" .. clock .. "\"}"

How to use Lua gsub to remove '<!--more--> correctly?

I have a string that might contains '<!--more-->' and I am trying to remove it by using this code
local str = string.gsub (string, "<!--more-->", "")
but it doesn't work. The string still contains it.
I try with this
local str = string.gsub (string, "[<!--more-->]", "")
but it has an 'e' left.
The - character is special in patterns. You need to escape it:
local str = string.gsub (string, "<!%-%-more%-%->", "")

Resources