Regex match anything except two specific strings (angular/cli 7.3.5, types/node 8.9.4) - node.js

I am using a regex expression to validate an input. I would like to match the exact strings "spe1" and "spe1.grl".
So far I have written the following code:
'\\b(?!i' + this.stringToIgnore + '\\b)\\w+';
where this.stringToIgnore = 'spe1'
this work if I type:
"spe1 " (note the space)
"spe1."
I would like to have a match as soon as I type:
"spe1" (without the need to add a space or dot at the end)
"spe1.grl" (without the need to add a space or dot at the end)
Thank you for your help

After palying a bit with an online Regex tool I have find the answer to my own question. I am posting it in case someone needs it:
'\\b(?!' + this.stringToIgnore + '\\b)\\w+';
or
''\\b(?!' + this.stringToIgnore + '|' + this.stringToIgnore.toUpperCase + '\\b)\\w+';
in case the user has the uppercase key on.

Related

Formula to extract number string between the nth instance of character to the next specific character (retrieving ID from URL)

I need to write a formula that can retrieve the number between the 4th "/" to the next "-"
Here's an example of the URL structure
https://www.example.com/category-name/1234-product-name
I've managed to get it done when the category name is ONE word like following URL with formula below.
https://www.example.com/category/1234-product-name
=MID(LEFT(A2,FIND("-",A2)-1),FIND("#",SUBSTITUTE(A2,"/","#",4))+1,255)
Result = 1234
The problem is that I can't rely on the category name always being one word... And the product name can also vary in characters so i can't start from right counting the "-"
Any tips & tricks? :)
You need the break the problem and then solve it.
=MID(B1,FIND("/",B1)+1, IFERROR(FIND("-",B1,FIND("/",B1)), LEN(B1) + 1) - FIND("/",B1)-1)
Explanation
=MID(TEXT, STARTING CHARACTER, NO OF CHARACTERS)
=MID(TEXT , WHERE FIRST '/' EXISTS , WHERE '-' EXISTS - WHERE FIRST '/' EXISTS )
and for removing the domain
=REPLACE(A1,1,LEN("https://www.example.com/"),"")

Deleting a string between two carriage returns tsql

Very new to SQL so I appreciate your patience in advance.
I have a column in a table that stores a particular set of instructions; each instruction is encapsulated by a carriage return.
eg: char(13)+ #instruction1 + char(13)...
#Instruction1 is a string of variable length but I do know a certain part of the string eg: #instruction1 = some string + #knownstring + some string.
So we have char(13) + (some string + #knownstring + some string) +char(13).
I want to replace this entire line with ''.
Identifying it just using the #knownstring.
Is this possible?
Thanking you all again, I really appreciate your assistance
select replace(replace(column,#knownsting,''),char(13),'')
from table
where key=1235
Replaces only the #knownstring but I also need to replace the surrounding text between the two char(13)
You might try something along this:
DECLARE #KnownString VARCHAR(50)='Keep This'
DECLARE #YourString VARCHAR(MAX)='blah' + CHAR(13) + 'dummy keep this dummy more' + CHAR(13) + 'Something without the known part' + CHAR(13) + 'Again with Keep THIS';
SELECT STUFF(
(
SELECT CHAR(13) + CASE WHEN CHARINDEX(#KnownString,LineText)>0 THEN #KnownString ELSE LineText END
FROM (SELECT CAST('<x>' + REPLACE(#YourString,CHAR(13),'</x><x>') + '</x>' AS XML)) A(Casted)
CROSS APPLY Casted.nodes('/x') B(fragment)
OUTER APPLY (SELECT fragment.value('text()[1]','nvarchar(max)')) C(LineText)
FOR XML PATH(''),TYPE
).value('.','nvarchar(max)'),1,1,'');
The result
blah
Keep This
Something without the known part
Keep This
The idea
The string is transformed to XML by replacing the line breaks with XML tags. Now we can query all text lines separately, check them for the known string, do the needed manipulation, and finally reconcatenate all fragments using the XML-trick (together with STUFF to get rid of the leading CHAR(13)).
Remarks
Using v2016 I'd use the split-string approach with OPENJSON and starting with v2017 there is STRING_AGG() to make the reconcatenation easier.

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'));

Lua string.match() problem

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]*)?"

Resources