How do I extract the right most number in a string? - ruby

I have strings like this:
https://www.facebook.com/username_with_number_14/posts/101505775425654414
https://www.facebook.com/username/posts/101505775425654466
I need to extract the number on the end of the string in Ruby. In the first string, it is the second and last number, whereas in the second string it is the first, only and last number.
At the moment I am extracting the number like this:
int1 = Regexp.new('.*?(\\d+)',Regexp::IGNORECASE).match()[1]
But when this is applied to the first string, it extracts the number part of the username, not the desired number.
How can I do it so that it will work on both strings?

text = <<ENDTEXT
https://www.facebook.com/username_with_number_14/posts/101505775425654414
https://www.facebook.com/username/posts/101505775425654466
ENDTEXT
p text.lines.map{|line| line.scan(/\d+/).last}
#=> ["101505775425654414", "101505775425654466"]

for me works regexp like this:
^.*?(\d+)$
look here: http://rubular.com/r/CJzsgjedqJ

Try this
int1 = Regexp.new('.*\\/(\\d+)$',Regexp::IGNORECASE).match()[1]
The $ matches the end of the string. So I put all numbers from the last / to the end of the string into the capturing group 1.

Related

Split String( String contains Regular expression)

I want to split the last string from given path, that string contains some numbers like 1.625.235, but this numbers vary every time. Irrespective of the number that last string should be split.
Ex:
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
output: Dynatrace-OneAgent-Windows-1.625.235.msi
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.181.539.msi"
output: Dynatrace-OneAgent-Windows-1.181.539.msi
This is what we tried
("C:/chef/cache/Dynatrace-OneAgent-Windows-/\d.\d+.\d+/.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'/\d.\d+.\d+/'.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'\d.\d+.\d+'.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'(\d.\d+.\d+').msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-('/\d.\d+.\d+/').msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-('\d.\d+.\d+').msi").split('/')[3]
If the output you want is always the filename at the end of a path, you could also use File.basename.
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
output = File.basename(string) # => "Dynatrace-OneAgent-Windows-1.625.235.msi"
string="C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
p string.split("/").last
output
"Dynatrace-OneAgent-Windows-1.625.235.msi"

Take the user input of numbers and print equivalent in the form of a -

I'm trying to take the user's input (numbers separated by commas, e.g., "5,8,11"), and return the equivalent number of "-"s. For example, if the user inputs "4,2,4,5", then the output should be the following:
----
--
----
-----
with each on a new line. I need to take an input string, split it at the commas, which will turn it into an array, and then iterate through the array and print the amount of commas per element.
I tried this,
puts "Enter some numbers"
input = gets.chomp
input.split(',')
input.each do |times|
puts "-" * times
end
which returns a noMethodError. I'm not sure where I am wrong.
Any help would be greatly appreciated.
You need integers for that. Try
input = gets.chomp.split(',').map(&:to_i)
Couple of things...
input.split(',')
This DOES split input, but it doesn't change the contents of the input variable.
What would work...
input = input.split(',')
Secondly, the result will be an array of strings, not integers, so better would be...
input = input.split(',').map(&:to_i)
This will map the string array into an integer array

In TI-BASIC, how do I add a variable in the middle of a String?

I am wondering how to make something where if X=5 and Y=2, then have it output something like
Hello 2 World 5.
In Java I would do
String a = "Hello " + Y + " World " + X;
System.out.println(a);
So how would I do that in TI-BASIC?
You have two issues to work out, concatenating strings and converting integers to a string representation.
String concatenation is very straightforward and utilizes the + operator. In your example:
"Hello " + "World"
Will yield the string "Hello World'.
Converting numbers to strings is not as easy in TI-BASIC, but a method for doing so compatible with the TI-83+/84+ series is available here. The following code and explanation are quoted from the linked page:
:"?
:For(X,1,1+log(N
:sub("0123456789",ipart(10fpart(N10^(-X)))+1,1)+Ans
:End
:sub(Ans,1,length(Ans)-1?Str1
With our number stored in N, we loop through each digit of N and store
the numeric character to our string that is at the matching position
in our substring. You access the individual digit in the number by
using iPart(10fPart(A/10^(X, and then locate where it is in the string
"0123456789". The reason you need to add 1 is so that it works with
the 0 digit.
In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used
for. Each time through the For( loop, we concatenate the string from
before (which is still stored in the Ans variable) to the next numeric
character that is found in N. Using Ans allows us to not have to use
another string variable, since Ans can act like a string and it gets
updated accordingly, and Ans is also faster than a string variable.
By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy
character to the string initially, we now need to remove it, which we
do by getting the substring from the first character to the second to
last character of the string. Finally, we store the string to a more
permanent variable (in this case, Str1) for future use.
Once converted to a string, you can simply use the + operator to concatenate your string literals with the converted number strings.
You should also take a look at a similar Stack Overflow question which addresses a similar issue.
For this issue you can use the toString( function which was introduced in version 5.2.0. This function translates a number to a string which you can use to display numbers and strings together easily. It would end up like this:
Disp "Hello "+toString(Y)+" World "+toString(X)
If you know the length of "Hello" and "World," then you can simply use Output() because Disp creates a new line after every statement.

string capture between duplicates in ruby

string = 'xabcdexfghijk'
In the example above, 'x' appears twice. I want to capture everything between the first 'x' and the next 'x'. Thus, the desired result is a new string that equals 'xabcdex'. Any ideas?
You could use a simple regular expression: /x.*?x/. This basically means "match any characters in between two x characters, as few times as possible (non-greedy)".
The matched text can be extracted with String#[regexp]
string = 'xabcdexfghijk'
string[/x.*?x/] # => "xabcdex"

copy the lines of a file into hashmap in ruby

I have a file with multiple lines. In each line, there two words and a number, split by a comma - for example a, b, 1. It means that string A and string B have the key as 1. I wrote the below piece of code
File.open(ARGV[0], 'r') do |f1|
while line = f1.gets
puts line
end
end
i'm looking for an idea of how to split and copy the characters and number in such a way that the first two words have the last number as key in the hashmap.
Does this work for you?
hash = {}
File.readlines(ARGV[0]).each do |line|
var = line.gsub(' ','').split(',')
hash[var[2]] = var[0],var[1]
end
This would give:
hash['1'] = ['a','b']
I don't know if you want to store number one as an integer or a string, if it's a integer you're looking for, just do var[2].to_i before storing.
Modified your code a little bit, i think it's shorter this way, if i'm in any way wrong, do let me know.

Resources