How to get part of string after some word with Ruby? - ruby

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.

Related

Replacing GUID in Kusto

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

How to use gsub regex in ruby

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"

Ruby newb: how do I extract a substring?

I'm trying to scrape a CBS sports page for shot data in the NBA.
Here is the page I'm starting out with and using as a sample: http://www.cbssports.com/nba/gametracker/shotchart/NBA_20131115_MIL#IND
In the source, I found a string that contains all the data that I need. This string, in the webpage source code, is directly under var CurrentShotData = new.
What I want is to turn this string in the source into a string I can use in ruby. However, I'm having some trouble with the syntax. Here's what I have.
require 'nokogiri'
require 'mechanize'
a = Mechanize.new
a.get('http://www.cbssports.com/nba/gametracker/shotchart/NBA_20131114_HOU#NY') do |page|
shotdata = page.body.match(/var currentShotData = new String\(\"(.*)\"\)\; var playerDataHomeString/m)[1].strip
print shotdata
end
I know I must be doing this wrong... it seems so needlessly complex and on top of that it isn't working for me. Could someone enlighten me on the simple way to get this string into Ruby?
Try to replace:
shotdata = page.body.match(/var currentShotData = new String\(\"(.*)\"\)\; var playerDataHomeString/m)[1].strip
with:
shotdata = page.body.match(/var currentShotData = new String\(\"(.*?)\"\)\; var playerDataHomeString/m)[1].strip
changing the (.*) with (.*?) will cause a lazy evaluation (matching of minimal number of characters) of the string which is the behavior you want.

Proper gsub regular expression for this URL?

Say I have a string representing a URL:
http://www.mysite.com/somepage.aspx?id=33
..I'd like to escape the forward slashes and the question mark:
http:\/\/www.mysite.com\/somepage.aspx\?id=33
How can I do this via gsub? I've been playing with some regular expressions in there but haven't hit on the winning formula yet.
I suggest you use
url = url.gsub(/(?=[\/?])/, '\\')
As shown here
url = 'http://www.mysite.com/somepage.aspx?id=33'
url = url.gsub(/(?=[\/?])/, '\\')
puts url
output
http:\/\/www.mysite.com\/somepage.aspx\?id=33
How about this one result = searchText.gsub(/(\/|\?)/, "\\\\$1")
I will suggest using a block to make it more readable:
url.gsub(/[\/?]/) { |c| "\\#{c}" }

Reflection in Ruby. Instantiate an object by given class name

I came to ruby from PHP.
How could i do the next thing in ruby?
$className = 'ArrayObject';
$arrayObject = new $className();
You can do this:
arrayObject = Object::const_get('Array').new
You can also use the following if you are using Ruby on Rails:
array_object = "Array".constantize.new
If you have a class, like for example String:
a = String
a.new("Geo")
would give you a string. The same thing applies to other classes ( number & type of parameters will differ of course ).

Resources