Lua string.match() problem - string

I want to match a few lines for a string and a few numbers.
The lines can look like
" Code : 75.570 "
or
" ..dll : 13.559 1"
or
" ..node : 4.435 1.833 5461"
or
" ..NavRegions : 0.000 "
I want something like
local name, numberLeft, numberCenter, numberRight = line:match("regex");
But I'm very new to the string matching.

This pattern will work for every case:
%s*([%w%.]+)%s*:%s*([%d%.]+)%s*([%d%.]*)%s*([%d%.]*)
Short explanation: [] makes a set of characters (for example the decimals). The last to numbers use [set]* so an empty match is valid too. This way the number that haven't been found will effectively be assigned nil.
Note the difference between using + - * in patterns. More about patterns in the Lua reference.
This will match any combination of dots and decimals, so it might be useful to try and convert it to a number with tonumber() afterwards.
Some test code:
s={
" Code : 75.570 ",
" ..dll : 13.559 1",
" ..node : 4.435 1.833 5461",
" ..NavRegions : 0.000 "
}
for k,v in pairs(s) do
print(v:match('%s*([%w%.]+)%s*:%s*([%d%.]+)%s*([%d%.]*)%s*([%d%.]*)'))
end

Here is a starting point:
s=" ..dll : 13.559 1"
for w in s:gmatch("%S+") do
print(w)
end
You may save these words in a table instead of printing, of course. And skip the second word.

#Ihf Thank you, I now have a working solution.
local moduleInfo, name = {};
for word in line:gmatch("%S+") do
if (word~=":") then
word = word:gsub(":", "");
local number = tonumber(word);
if (number) then
moduleInfo[#moduleInfo+1] = number;
else
if (name) then
name = name.." "..word:gsub("%$", "");
else
name = word:gsub("%$", "");
end
end
end
end
#jpjacobs Really nice, thanks too. I'll rewrite my code for synthetic reasons ;-) I'll implement your regex of course.

I have no understanding of the Lua language, so I won't help you there.
But in Java this regex should match your input
"([a-z]*)\\s+:\\s+([\\.\\d]*)?\\s+([\\.\\d]*)?\\s+([\\.\\d]*)?"
You have to test each group to know if there is data left, center, right
Having a look at Lua, it could look like this. No guarantee, I did not see how to escape . (dot) which has a special meaning and also not if ? is usable in Lua.
"([a-z]*)%s+:%s+([%.%d]*)?%s+([%.%d]*)?%s+([%.%d]*)?"

Related

python3 replace ' in a string

I am trying to clean text strings containing any ' or &#39 (which includes an ; but if i add it here you will see just ' again. Because the the ANSI is also encoded by stackoverflow. The string content contains ' and when it does there is an error.
when i insert the string to my database i get this error:
psycopg2.ProgrammingError: syntax error at or near "s"
LINE 1: ...tment and has commenced a search for mr. whitnell's
the original string looks like this:
...a search for mr. whitnell&#39s...
To remove the ' and &#39 ; I use:
stripped_content = stringcontent.replace("'","")
stripped_content = stringcontent.replace("&#39 ;","")
any advice is welcome, best regards
When you try to replace("&#39 ;","") it literally searching for "&#39 ;" occurrences in string. You need to convert "&#39 ;" to its character equivalent. Try this:
s = "That's how we 'roll"
r = s.replace(chr(int('&#39'[2:])), "")
and with this chr(int('&#39'[2:])) you'll get ' character.
Output:
Thats how we roll
Note
If you try to run this s.replace(chr(int('&#39'[2:])), "") without saving your result in variable then your original string would not be affected.

Regex take text after ":" excluding ":"

I have this string:
194.44.176.116:8080
I want regex to take everything after the colon ':' but not take : itself. How do I do that?
This is something I did but it grabs numbers and the colon ':' which I don't want.
var portRe = /(?<=:)\d{2,5}$/gi;
I'm using this in NodeJs application.
Your regex to get everything between the : and the end would be:
/[^:]+$/
But as we know,that the port is a number, you can just check for the number at end of string:
/[0-9]+$/
Please note that this does not check if there is a : and so just returns the last digit. If you are sure that you have a string as you provided, those two are the easiest to understand minimalist solutions.
Otherwise refer to the other answers to do a lookahead/lookbehind or work with non capturing / capturing groups.
A general strategy here would be to just use a capture group to isolate what you really want to match:
/.*:(\d{2,5})/gi
Then access what you have captured in the first capture group.
var myString = "194.44.176.116:8080";
var myRegexp = /.*:(\d{2,5})/gi;
var match = myRegexp.exec(myString);
console.log(match[1]);

How to replace part of a string with an added condition

The problem:
The objective is to convert: "tan(x)*arctan(x)"
Into: "np.tan(x)*np.arctan(x)"
What I've tried:
s = "tan(x)*arctan(x)"
s = s.replace('tan','np.tan')
Out: np.tan(x)*arcnp.tan(x)
However, using pythons replace method resulted in arcnp.tan.
Taking one additional step:
s = s.replace('arcnp.', 'np.arc')
Out: np.tan(x)*np.arctan(x)
Achieves the desired result... but this solution is sloppy and inefficient.
Is there a more efficient solution to this problem?
Any help is appreciated. Thanks in advance.
Here is a way to do the job:
var string = 'tan(x)*arctan(x)';
var res = string.replace(/\b(?:arc)?tan\b/g,'np.$&');
console.log(res);
Explanation:
/ : regex delimiter
\b : word boundary, make sure we don't have any word character before
(?:arc)? : non capture group, literally 'arc', optional
tan : literally 'tan'
\b : word boundary, make sure we don't have any word character after
/g : regex delimiter, global flag
Replace:
$& : means the whole match, ie. tan or arctan
You can use regular expression to solve your issue. Following code is in javascript. Since, u didn't mention the language you are using.
var string = 'tan(x)*arctan(x)*xxxtan(x)';
console.log(string.replace(/([a-z]+)?(tan)/g,'np.$1$2'));

Find index of a specific character in a string then parse the string

I have strings which looks like this [NAME LASTNAME/NAME.LAST#emailaddress/123456678]. What I want to do is parse strings which have the same format as shown above so I only get NAME LASTNAME. My psuedo idea is find the index of the first instance of /, then strip from index 1 to that index of / we found. I want this as a VBScript.
Your way should work. You can also Split() your string on / and just grab the first element of the resulting array:
Const SOME_STRING = "John Doe/John.Doe#example.com/12345678"
WScript.Echo Split(SOME_STRING, "/")(0)
Output:
John Doe
Edit, with respect to comments.
If your string contains the [, you can still Split(). Just use Mid() to grab the first element starting at character position 2:
Const SOME_STRING = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(Split(SOME_STRING, "/")(0), 2)
Your idea is good here, you should also need to grab index for "[".This will make script robust and flexible here.Below code will always return strings placed between first occurrence of "[" and "/".
var = "[John Doe/John.Doe#example.com/12345678]"
WScript.Echo Mid(var, (InStr(var,"[")+1),InStr(var,"/")-InStr(var,"[")-1)

Alternative to CONTAINS method?

Giving the string below with "server type" separated by comma:
string serverTypeList = "DB, IIS, CMDB";
//server.Type in the loop below should have value of "MDB"
My problem is that in this scenario it will return TRUE because "MDB" string is inside the serverTypeList.
I need it to return TRUE only if it matches a type of "MDB" and not "CMDB":
...
from site in SiteManager.Sites
from server in site.Servers
where
serverTypeList.Contains(server.Type)
select new Server()
{ ID=server.ID, SiteName=site.Name }
...
How can I change the code above?
Thank you
(", " + serverTypeList + ", ").Contains(", " + server.Type + ", ")
is one standard way to handle this. I'm not clear on the language you're using, so I don't know the exact syntax you would need, but the general idea is to ensure that the term appears between delimiters by forcing delimiters before and after the list string.

Resources