How do I extract the prefix of a string till the last instance of a specific character? - kusto-explorer

for eampaple, the specific character is "_":
let str = "a_b_c_d_1";
how can I extract "a_b_c_d" ?

Found a way to do so, I believe there are more elegant ones.
let str = "a_b_c_d_1";
print(strcat_array(array_shift_right(split(str, "_"), 1),""))

Related

best way to find substring in ruby using regular expression

I have a string https://stackverflow.com. I want a new string that contains the domain from the given string using regular expressions.
Example:
x = "https://stackverflow.com"
newstring = "stackoverflow.com"
Example 2:
x = "https://www.stackverflow.com"
newstring = "www.stackoverflow.com"
"https://stackverflow.com"[/(?<=:\/\/).*/]
#⇒ "stackverflow.com"
(?<=..) is a positive lookbehind.
If string = "http://stackoverflow.com",
a really easy way is string.split("http://")[1]. But this isn't regex.
A regex solution would be as follows:
string.scan(/^http:\/\/(.+)$/).flatten.first
To explain:
String#scan returns the first match of the regex.
The regex:
^ matches beginning of line
http: matches those characters
\/\/ matches //
(.+) sets a "match group" containing any number of any characters. This is the value returned by the scan.
$ matches end of line
.flatten.first extracts the results from String#scan, which in this case returns a nested array.
You might want to try this:
#!/usr/bin/env ruby
str = "https://stackoverflow.com"
if mtch = str.match(/(?::\/\/)(/S)/)
f1 = mtch.captures
end
There are two capturing groups in the match method: the first one is a non-capturing group referring to your search pattern and the second one referring to everything else afterwards. After that, the captures method will assign the desired result to f1.
I hope this solves your problem.

Reformatting dates

I'm trying to reformat German dates (e.g. 13.03.2011 to 2011-03-13).
This is my code:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
str = str.gsub("/(\d{2}).(\d{2}).(\d{4})/", "/$3-$2-$1/")
I get the same output like input. I also tried my code with and without leading and ending slashes, but I don't see a difference. Any hints?
I tried to store my regex'es in variables like find = /(\d{2}).(\d{2}).(\d{4})/ and replace = /$3-$2-$1/, so my code looked like this:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
find = /(\d{2}).(\d{2}).(\d{4})/
replace = /$3-$2-$1/
str = str.gsub(find, replace)
TypeError: no implicit conversion of Regexp into String
from (irb):4:in `gsub'
Any suggestions for this problem?
First mistake is the regex delimiter. You do not need place the regex as string. Just place it inside a delimiter like //
Second mistake, you are using captured groups as $1. Replace those as \\1
str = str.gsub(/(\d{2})\.(\d{2})\.(\d{4})/, "\\3-\\2-\\1")
Also, notice I have escaped the . character with \., because in regex . means any character except \n

Splitting A String In Two Parts(REALBASIC)

I am trying to split a string at the character ":" but cant create two separate strings from the split. If somebody could help me, I would appreciate it.
In RealBasic, the Split method doesn't create two (or more) separate strings but rather a single string array.
Dim s() As String = Split("Zero:One:Two", ":")
's() now contains the substrings like so:
's(0) = "Zero"
's(1) = "One"
's(2) = "Two"
Actually, the code is incorrect. It should be:
Dim s() As String = Split("Zero:One:Two", ":")
If you don't pass in the delimiter it assumes a space which wouldn't work in this case.
The online docs are at http://docs.realsoftware.com/index.php/Split
Split is best for actually splitting the text, but you can also use the string-manipulation methods: Left, Right, Mid and InStr.

What is the best way to get 51, out of the following string

What is the best way to get '51', out of the following string in ruby :
"<https://api.example.com/users/lgs/api?page=2>; rel=\"next\", <https://api.example.com/users/lgs/api?page=51>; rel=\"last\""
Thanks in advance
Luca
If you know there're only two numbers in that string then this is enough:
str = '"<https://api.example.com/users/lgs/api?page=2>; rel=\"next\", <https://api.example.com/users/lgs/api?page=51>; rel=\"last\""'
p str.scan(/\d+/).last #=> "51"
If not then provide more detail to make the regex more precise. Also you can add to_i if you need the answer as a number.
You can use regexp in this way:
res = str.match /.page=(\d+)./
in this way you are "capturing all the digits between "(" and ")" (in the last token), and you result will be store in
res.captures.first
(or simply in $1 variable)

Check that a string exists within another string with regular expression

I've got a regular expression that I am using to strip an extension from another string
The extensions in this example are
BK|BZ|113
If the string does not contain any of the extensions, then I need it to leave the string as is.
The regular expression I'm using is
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1]
However, if the base_value does not contain the string, I want to return the base_value. I thought I could use
base_value = base_string
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] unless (base_value !~ /(.*?[^-])-?(?:BK|BZ|113)) == false
but that isn't working.
What is the best way to return the base_string if the extensions are not found?
If I understand well, you want to strip the regexp from a string, if it matches, right? So.. do it:
string.sub(/(?:BK|BZ|113)$/, "")
Perhaps you can use sub like this:
base_string1 = "test1BK"
base_string2 = "test2"
p base_string1.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
p base_string2.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
When the pattern is not found nothing is replaced, otherwise it returns the string without the extension.
If /(.*?[^-])-?(?:BK|BZ|113)/ doesn't match then base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] will be nil. So this should work:
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] || base_string

Resources