0

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]
9
  • 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] Commented May 3, 2019 at 11:16
  • what is the output you required? Commented May 3, 2019 at 11:34
  • I want like this "Dynatrace-OneAgent-Windows-1.181.539.msi" Commented May 3, 2019 at 11:40
  • 'C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi'.split('/').last try this Commented May 3, 2019 at 11:49
  • I know this, but in "Dynatrace-OneAgent-Windows-1.625.235.msi'" those numbers will changing every time, some times it is Dynatrace-OneAgent-Windows-1.625.235.msi' and some time it is Dynatrace-OneAgent-Windows-1.181.539.msi, like that Commented May 3, 2019 at 12:16

2 Answers 2

4

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Not that, I want "Dynatrace-OneAgent-Windows-x.xxx.xxx.msi" , in "Dynatrace-OneAgent-Windows-1.625.235.msi'" those numbers will changing every time, some times it is Dynatrace-OneAgent-Windows-1.625.235.msi' and some time it is Dynatrace-OneAgent-Windows-1.181.539.msi, In this path "C:/chef/cache/" that msi file will be change every time with different versions. Irrespective of version i want pick that msi file......
It is work, if i hard coded that numbers in then given string. but i don;t wan that...
I'm not sure what problem you're having. The value of string is hardcoded in this example for demonstration purposes. Its value could come from anywhere, and File.basename should return "Dynatrace-OneAgent-Windows-x.xxx.xxx.msi" regardless of what the numbers are.
So change the argument you send to File.basename to whatever the source of the value is. File.basename(read_line_from_file). It doesn't matter. The string variable is ONLY an example.
2
string="C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"

p string.split("/").last

output

"Dynatrace-OneAgent-Windows-1.625.235.msi"

1 Comment

Not that, I want "Dynatrace-OneAgent-Windows-x.xxx.xxx.msi" , in "Dynatrace-OneAgent-Windows-1.625.235.msi'" those numbers will changing every time, some times it is Dynatrace-OneAgent-Windows-1.625.235.msi' and some time it is Dynatrace-OneAgent-Windows-1.181.539.msi, In this path "C:/chef/cache/" that msi file will be change every time with different versions. Irrespective of version i want pick that msi file....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.