Using wildcards in a Ruby string - ruby

Let's say I have something like:
Image.from_file("path/to_file/#{$variable_name}_[some_number].png")
I don't know what [some_number] will be, but I know that it will always be there.
How can I write a regex (presumably) so that it doesn't matter what [some_number] is?
Thanks.
--- edit ---
#DavidGrayson you're right. I don't need a regex at all (sorry for the wild goose chase folks).
Looks like I can get exactly what I need from newFileName = Dir.glob("#{variable_name}_*.png").first then Image.from_file("path/to_file/#{$newFileName}") etc.
Thanks to everyone who tried to help.

You can have variables in regexes in ruby: /#{$variable_name}_\d+\.png/

Related

Ruby regex section to match multiline

So this is my code
convert = contents.gsub(/\\s1(.*?)(\n\\r.*?)?\n((?s)\\ms3(.*?)\\p)/, 'replacement code')
in the first bit: \\s1(.*?)(\n\\r.*?)?\ni only want it to match a newline when i tell it there's one there. But when searching for \\ms3(.*?)\\p i want it to pick up any newlines that are there. Unfortunately it looks like Ruby doesn't support this (?s)prefix. Is there any way of doing this?
thanks
(.*?)==>([\s\S]*?)
You can use this instead of DOTALL modifier.
convert = contents.gsub(/\\s1(.*?)(\n\\r.*?)?\n((\n*)\\ms3(.*?)\\p)/, 'replacement code')
This will capture any(0+) newlines before "\ms3". If it's not what you meant, please, clarify what functionality do you expect from (?s)?

Ruby: Rubeque: Variable in regexp?

I'm solving http://www.rubeque.com/problems/a-man-comma--a-plan-comma--a-canal--panama-excl-/solutions but I'm a bit confused about treating #{} as comment in regexp.
My code look like this now
def longest_palindrome(txt)
txt[/#{txt.reverse}/]
end
I tried txt[/"#{txt.reverse}"/] or txt[#{txt.reverse}] but nothing works as I wish. How should I implicate variable into regexp?
This is not something you can do with a regex.
While you could use variable interpolation in the construction of a regex (see the other answers/comments), that wouldn't help you here. You could only use that to reverse a literal string, not a regex match result. Even if you could, you still wouldn't have solved the "find the longest palindrome" part, at least not with acceptable runtime performance.
Use a different approach to the problem.
It is hard to tell how do you wish that happens without examples, but I suppose you are after
txt[/#{Regexp.escape(txt.reverse)}/]
See the Regexp#escape method

Trying to remove \"id\":x from json string using regular expression in Ruby

I have a JSON string that looks like {\"heading\":\"Test\",\"id\":1} and I want to wipe the ID data from the string.
I've tried test.gsub(/\,\\"id\\"\:d+/, '') but that's not working.
How best to achieve this?
Sergio's JSON.parse is something you should consider. But baring that, those \'s you are seeing probably aren't really part of the string. That's just how irb is displaying it.
So test.gsub(/,"id":\d+/, '') should be what you want. (Also fixed a few other small bugs in the regex).

Perfect way to write a gsub for a regex match?

I am trying to write a gsub for a regex match, but I imagine there's a more perfect way to do this .
My equation :
ref.gsub(ref.match(/settings(.*)/)[1], '')
So that I can take this settings/animals, and return just settings.
But what if settings is null? Than my [1] fails as expected.
So how can one write the above statement assuming that sometimes settings won't match ?
So that basically, if it finds the word, settings, than get rid of anything after it. But if it doesn't, no worries.
Thanks!
Why not do the simplest possible thing that could work?
ref.gsub(/(settings)(.*)/, '\1')

Ruby regex: extract a list of urls from a string

I have a string of images' URLs and I need to convert it into an array.
http://rubular.com/r/E2a5v2hYnJ
How do I do this?
URI.extract(your_string)
That's all you need if you already have it in a string. I can't remember, but you may have to put require 'uri' in there first. Gotta love that standard library!
Here's the link to the docs URI#extract
Scan returns an array
myarray = mystring.scan(/regex/)
See here on regular-expressions.info
The best answer will depend very much on exactly what input string you expect.
If your test string is accurate then I would not use a regex, do this instead (as suggested by Marnen Laibow-Koser):
mystring.split('?v=3')
If you really don't have constant fluff between your useful strings then regex might be better. Your regex is greedy. This will get you part way:
mystring.scan(/https?:\/\/[\w.-\/]*?\.(jpe?g|gif|png)/)
Note the '?' after the '*' in the part capturing the server and path pieces of the URL, this makes the regex non-greedy.
The problem with this is that if your server name or path contains any of .jpg, .jpeg, .gif or .png then the result will be wrong in that instance.
Figuring out what is best needs more information about your input string. You might for example find it better to pattern match the fluff between your desired URLs.
Use String#split (see the docs for details).
Part of the problem is in rubular you are using https instead of http.. this gets you closer to what you want if the other answers don't work for you:
http://rubular.com/r/cIjmjxIfz5

Resources