I want to remove some part of string from using ruby regex:
value = localhost:8393/foobar/1 test:foobartest
I want to remove "test" from my string [localhost:8393/foobar/1 test:foobartest] and rest of the value so that output should look like:
localhost:8393/foobar/1
How to do this in ruby? Can you share some sample code to achieve this?
Appreciated your help in advance!
Thanks!
I would do something like this:
value = 'localhost:8393/foobar/1 test:foobartest'
value.split.first
#=> "localhost:8393/foobar/1"
Or if you want to use an regexp:
value.sub(/ test.*/, '')
"localhost:8393/foobar/1"
Related
How can I replace all GUID's in a Kusto query with no value.
e.g.
my data looks like
/page/1d58e797-a905-403f-ebd9-27ccf3f1d2cd/user/4d58e797-a905-403f-ebd9-27ccf3f1d2c3
and I want
/page//user/
You can use the replace function. Also, you can test regular expression here.
let input = '/page/1d58e797-a905-403f-ebd9-27ccf3f1d2cd/user/4d58e797-a905-403f-ebd9-27ccf3f1d2c3';
let rx = '[({]?[a-fA-F0-9]{8}[-]?([a-fA-F0-9]{4}[-]?){3}[a-fA-F0-9]{12}[})]?';
print replace(rx, '', input);
Try using the below regex It will remove guid (8-4-4-4-12) in url
let regex =/(\/[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})(\b|\/)/g
Is it possible to set points in initials?
For example to change MAW into M.A.W.
I tried keep_before, but it doesn't work.
?keep_before(" ")+". "}
Result: MAW.
Please help.
You could do it like:
${'MAW'?replace('','.')[1..]}
'MAW'?replace('','.') will result in .M.A.W., which you can "substring" by using the range [1..].
See
https://freemarker.apache.org/docs/ref_builtins_string.html#ref_builtin_replace
https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_stringop_slice
It's easiest to do with regular expressions: ${initials?replace('.', '$0.', 'r')}. It's maybe nicer if you wrap this into a #function though (<#function dotify(s)><#return s?replace('.', '$0.', 'r')></#function>, and then ${dotify(initals)}), especially if you need to do this on multiple places.
If your letters are in name try:
<#list 0..(name?length-1) as idx>${name[idx]}.</#list>
I have params like:
params[:id]= "\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\""
And i want to get expected result as below:
"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
How can I do this?
You can use gsub:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".gsub("\"", "")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
Or, as #Stefan mentioned, delete:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".delete("\"")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
If this is JSON data, which it could very well be in that format:
JSON.load(params[:id])
This handles things where there's somehow escaped strings in there, or the parameters are an array.
Just Use tr!
params[:id].tr!("\"","")
tr! will also change the main string
In case you do not want to change main string just use :
params[:id].tr("\"","")
Thanks Ilya
I have a string containing a path:
/var/www/project/data/path/to/file.mp3
I need to get the substring starting with '/data' and delete all before it. So, I need to get only /data/path/to/file.mp3.
What would be the fastest solution?
'/var/www/project/data/path/to/file.mp3'.match(/\/data.*/)[0]
=> "/data/path/to/file.mp3"
could be as easy as:
string = '/var/www/project/data/path/to/file.mp3'
path = string[/\/data.*/]
puts path
=> /data/path/to/file.mp3
Using regular expression is a good way. Though I am not familiar with ruby, I think ruby should have some function like "substring()"(maybe another name in ruby).
Here is a demo by using javascript:
var str = "/var/www/project/data/path/to/file.mp3";
var startIndex = str.indexOf("/data");
var result = str.substring(startIndex );
And the link on jsfiddle demo
I think the code in ruby is similar, you can check the documentation. Hope it's helpful.
Please try this:
"/var/www/project/data/path/to/file.mp3".scan(/\/var\/www(\/.+)*/)
It should return you all occurrences.
there is a string like this and it is stored in a file
#{date}abcde.doc
I want to be able to read this string and replace #{date} with
Date.today.strftime("%Y%m%d")
Is there any way to parse the template and do the evaluation? Thanks in advance!
Yes, however...
It would be easier if you used hash replacement, like this:
s = "%{date}abcde.doc"
s % { date: Time.now.strftime(etc) }
Or just use ERb.
As-is you're using string interpolation so it would need to be evaled, I think.