Lua - Get first occurrance of substring using a pattern - string

I have a string that looks like:
local str = "rootFolder\\<subFolder>\\<...>\\nFolder\\fileName";
where <...> could be a list of other folder names making the path/string very long. Also, I do not know what <subFolder> will actually be called since the folder name could be anything, i.e:
rootFolder\\folderA\\...
rootFolder\\folderB\\...
rootFolder\\folderC\\...
...
We can assume I know the name of the root folder because this will be known at runtime, so for now lets assume it is called rootFolder.
How can I extract the sub-string <subFolder> using a pattern to match against str?
I was thinking of something like:
string.match(str, "rootFolder\\(.*)\\.*"); to capture the first folderName under rootFolder in the folder/directory hierarchy and ignore anything else that follows it but this is not working because, although it does match, it also gets everything else that follows it and not just the part I need (I also tried using .+ instead of .*).
For example, I want to be able to do this:
local str = "rootFolder\\hello\\anotherFolder\\myFile";
-- this pattern does not work as expected:
local folderName = string.match(str, "rootFolder\\(.*)\\.*");
print(folderName == "hello"); -- true
Hope that makes sense. Thank you.

The answer was to use a minus:
local folderName = string.match(str, "rootFolder\\(.-)\\");

Related

How would I do pattern matching on this string in lua?

I have a string which contains a path to a file:
C:\Users\user\directory\build.bat
Is there a way I can remove the word between the last backslash and the second last backslash (in this case it is directory), however I would like a way to do this several times:
First time I run the code the path should look like this:
C:\Users\user\build.bat
If I run the code on the new string I just got from running the program the first time, the output should be like this:
C:\Users\build.bat
Yes, this is very much possible. I propose the following pattern:
^(.+)\.-\(.-)$ - this will match the entire path, matching first the directories up to the parent directory and then the filename. Use it as follows:
local function strip_parent_dir(path)
local path_to_parent, filename = path:match[[^(.+)\.-\(.-)$]]
return path_to_parent .. "\\" .. filename
end
which can then be used as follows:
print(strip_parent_dir[[C:\Users\user\directory\build.bat]]) -- C:\Users\user\build.bat
print(strip_parent_dir(strip_parent_dir[[C:\Users\user\directory\build.bat]])) -- C:\Users\build.bat
another option would be to split the path by the path delimiter (backslash), inserting the parts into a table, then removing the penultimate entry, finally concatenating the parts; this is slightly longer but may be considered more readable (and it may have better asymptotic performance):
local function strip_parent_dir(path)
local parts = {}
for part in path:gmatch"[^\\]+" do
table.insert(parts, part)
end
table.remove(parts, #parts - 1)
return table.concat(parts, "\\")
end
the results are the same. Choose whatever you consider more readable.

Replacing a certain part of string with a pre-specified Value

I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"

Go how can i efficient split string into parts

i am fighting with a string splitting. I want to split string by wildcards into a slice, but this slice should contain this wildcards as well.
For example: /applications/{name}/tokens/{name} should be split into [/applications/ {name} /tokens/ {name}] etc.
Here is a sample code i wrote, but it is not working correctly, and i don't feel good about it either.
https://play.golang.org/p/VMOsJeaI4l
There are some example routes to be tested. Method splitPath split path into parts and display both: before and after.
Here is a solution:
var validPathRe = regexp.MustCompile("^(/{[[:alpha:]]+}|/[-_.[:alnum:]]+)+$")
var splitPathRe = regexp.MustCompile("({[[:alpha:]]+}|[-_.[:alnum:]]+)")
func splitPath(path string) parts{
var retPaths parts
if !validPathRe.MatchString(path) {
return retPaths
}
retPaths = splitPathRe.FindAllString(path, -1)
return retPaths
}
I made this by creating two regular expressions, one to check if the path was valid or not, the other to extract the various parts of the path and return them. If the path is not valid it will return an empty list. The return of this will look like this:
splitPath("/path/{to}/your/file.txt")
["path" "{to}" "your" "file.txt"]
This doesn't include the '/' character because you already know that all strings in the return but the last string is a directory and the last string is the file name. Because of the validity check you can assume this.

Is stringA in StringB in MATLAB

Is there a way to check if a string exists within another string in MATLAB. In python this is done easily with a in b. I do not want indexes or anything like that. I just want to check if its true or not. The answers that I find is "strcmp" or "strfind" and also regexp. regexp returns indexes. strcmp(a, b) does not seem to work. I have a string a = 'ac' and another string b = 'bc_gh_ac'. And want to check if a in b.
Best regards
The answer is indeed strfind. You have to be careful with the order of the parameters which at first seems unusual - the pattern is the second argument, not the first. The following code demonstrates:
a='ac';
b='bc_gh_ac';
strfind(b,a)
If you simply want to test whether the string is present or not then use the isempty function:
if ~isempty(strfind(b,a))
disp('String is present');
end

How to find and replace a string in Matlab

So here is my problem:
I have a list of names in Matlab in a cell array.
I automatically create directories and .mat files for each name.
My problem is that some of these names contains '/' and therefore everything go wrong when I create the directory…
So I am trying to find an efficient way to find '/' and replace them.
So far I've tried to find them using the findstr function. It then gives me a cell array with the indexes where '/' appears. So when the name doesn't contain any '/' it returns {[]} and when the function find it, it returns {[i]}.
Now i'd like to have a logical condition that says if findstr is not empty then do something. I've tried with the isempty function but it doesn't work (it's never empty…)
So does anyone have a solution to this?
Thanks
Use regexprep to replace the character:
list = {'aaa', 'bb/cc', '/dd/'};
replace_from = '/'; %// character to be replaced
replace_to = '_'; %// replacing character
list_replaced = regexprep(list, replace_from, replace_to);
gives
list_replaced =
'aaa' 'bb_cc' '_dd_'

Resources