Regular Expressions in Ruby issue [closed] - ruby

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a string like this /4/LM2301_800.mp4.
I would like to use a regex to strip the /4/ and the _800 from the file name.
I am currently trying to use the strip command. Can anyone point me in the right direction?

Your file could be deeply nested inside a directory structure:
path = "/4/LM2301_800.mp4"
path.sub(/^(\/.)*\/(.*)_\d+\.mp4/, $2)
=> "LM2301" # you already know these are mp4 files, so you could add the suffix
Or:
path = "/4/LM2301_800.mp4"
path.sub(/^(\/.)*\/(.*)_\d+\.(.*)/, $2+'.'+$3)
=> "LM2301.mp4"

" /4/LM2301_800.mp4".scan(/.*\/(.*?)_\d*(.*)/).join
=> "LM2301.mp4"

Related

Opening a file in ruby [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I tried opening a file in ruby with the following code but I got this unknown error message.
No such file or directory - -p
Code:
myfile=File.open("/home/cucumber/profiles/data.csv","r")
Please advice with the correct method to do.
Ensure whether file exists in the designated location or not, either you check it manually or use in code:
File.exists?("/home/cucumber/profiles/data.csv")

How do I read all the file names of a directory into an array? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to store all the filenames inside a folder into an array. What's the best way to do this?
You can use this:
files = Dir.foreach(dir).select { |x| File.file?("#{dir}/#{x}") }
This returns the filenames, i.e. without folder.
If you need the complete path, use something like this:
files = Dir.foreach(dir) \
.map { |x| File.expand_path("#{dir}/#{x}") } \
.select { |x| File.file?(x) }
You can use:
files = Dir.entries(directory)
that returns an array containing all the filenames in the given directory.
Take a look in the Ruby Doc for more information.
you can also use files=Dir.glob(*).

Regular expression required for string [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to solve below problem using regular expression. My input string is something like this
"MTSGNN0002( 1), MTSGNN0028( 645), MTSGNN0050( 10)"
and I want output like this
"MTSGNN0002,MTSGNN0028,MTSGNN0050"
It should delete all charecter which comes between brackets. Kindly help me out in solving this.
Sometimes it's simpler to find what you want than delete what you don't:
s = "MTSGNN0002( 1), MTSGNN0028( 645), MTSGNN0050( 10)"
s.scan(/MT\w+/).join(',') # Change MT to whatever suits your data.
Look at this regexp:
([a-zA-Z0-9]*)\(.*\)
http://rubular.com/r/maZNs0mDkv
From there on it's easy ;-)
Here you go..
\([^\)]*\)|\s
As per your question.. The above replaces the brackets and the text inside and remove the spaces.
Example and Source Demo:
Here i have got one more solution
([(\b]\s.[0-9]*\b\))
http://rubular.com/r/9NyoU3RKUT
replace mathes of this regex \([ \d]+\) with empty string

What would be regex for this [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have list of website, I want all website with url http://www.tutorspree.com/tutor/#{AnyNumber) where after 'tutor' there could be any number. How do I write Regex for this?
I would define it with:
Regexp.new("^http://www\.tutorspree\.com/tutor/\\d+$")
This avoids having to escape the forward slashes. It would be used on a list of URLs like this:
tutor_re = Regexp.new("^http://www\.tutorspree\.com/tutor/\\d+$")
list = [ "http://nomatch.com/", "http://www.tutorspree.com/tutor/1", "http://www.tutorspree.com/tutor/2" ]
matches = list.select { |url| tutor_re.match url }
Something like:
http:\/\/www\.tutorspree\.com\/tutor\/\#\d+
ought to work.

How to set form_validation error messages for 3 different languages in codeigniter? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm thinking about duplicating the system\english\form_validation_lang.php file or folder, but I don't know how could I work that out.
For new languages, you should just create a new form_validation_lang.php file inside each language folder, like:
application/language/portugues/form_validation_lang.php
application/language/espanol/form_validation_lang.php
application/language/french/form_validation_lang.php
Then, you just have to set the appropriate language and Codeigniter will fetch the translations according to the language set.
More information can be found in the Codeigniter User Guide

Resources