Parsing between quotes in Ruby - ruby

I'm trying to parse a string, but I'm running into trouble:
str = " #Var(\"^This is the text I want to save.\") "
What I want to parse out is: ^This is the text I want to save. Basically anything that's between the quotes.
I was trying to use a str.split()... but I'm unsure about what to specify.
Thanks

You can use regex. Here:
puts str.scan(/"(.*)"/)[0][0]
# ^This is the text I want to save.
The above regex basically looks for everything that is between " and " and returns them.

This should work - assuming you don't expect there to be multiple quoted sections. The 1 specifies the match group you want.
puts str[/"(.*)"/, 1]
# ^This is the text I want to save.

If you want to get array of arrays of different size, you may do it in two steps: .split and .scan. In your case .scan has () on two sides of |, that's why you have trouble with nil (Which supposed to be useful, but not it your case). So you have either use .flatten.compact or add the 3rd step of .delete.
Try this:
text.split("\n").map{|i|p i.scan(/'([^']+)'|(\w+)/).flatten.compact}
text.split("\n").map{|i|p i.scan(/'[^']+'|\w+/).map{|i|i.delete "'"}}

Related

Remove quotation marks from an object

I want to call User.first, but I get it like "User.first". How can I strip the quotation marks so I can call User? Using a regex like this: gsub!(/\A"|"\Z/, "") returns nil instead of the expression.
First I would say that it's dangerous to do this based on where your input is coming from, but if you absolutely need to run arbitrary ruby code contained in a string, you would use eval:
http://ruby-doc.org/core-2.2.2/Kernel.html#method-i-eval
Again, I would avoid evaluating strings if at all possible.

How do I filter file names out of a SQLite dump?

I'm trying to filter out all file names from an SQLite text dump using Ruby. I'm not very handy/familiar with regex and need a way to read, and write to a file, another dump of image files that are within the SQLite dump. I can filter out everything except stuff like this:
VALUES(3,5,1,43,'/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG','1415',NULL);
and this:
src="/images/9/94/folder%2FGraph.JPG"
I can't figure out the easiest way to filter through this. I've tried using split and other functions, but instead of splitting the string into an array by the character specified, it just removed the character.
You should be able to use .gsub('%2', ' ') the %2 with a space, while quoted, it should be fine.
Split does remove the character that is being split, though. So you may not want to do that, or if you do, you may want to use the Array#join method with the argument of the character you split with to put it back in.
I want to 'extract' the file name from the statements above. Say I have src="/images/9/94/folder%2FGraph.JPG", I want folder%2FGraph.JPG to be extracted out.
If you want to extract what is inside the src parameter:
foo = 'src="/images/9/94/folder%2FGraph.JPG"'
foo[/^src="(.+)"/, 1]
=> "/images/9/94/folder%2FGraph.JPG"
That returns a string without the surrounding parenthesis.
Here's how to do the first one:
bar = "VALUES(3,5,1,43,'/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG','1415',NULL);"
bar.split(',')[4][1..-2]
=> "/images/e/e5/Folder%2FOrders%2FFinding_Orders%2FView_orders3.JPG"
Not everything in programming is a regex problem. Somethings, actually, in my opinion, most things, are not candidates for a pattern. For instance, the first example could be written:
foo.split('=')[1][1..-2]
and the second:
bar[/'(.+?)'/, 1]
The idea is to use whichever is most clean and clear and understandable.
If all you want is the filename, then use a method designed to return only the filename.
Use one of the above and pass its output to File.basename. Filename.basename returns only the filename and extension.

Using Ruby on a string, how can I slice between two parts of the string using RegEx?

I just want to save the text between two specific points in a string into a variable. The text would look like this:
..."content"=>"The text I want to save to a variable"}]...
I suppose I would have to use scan or slice, but not exactly sure how to pull out just the text without grabbing the RegEx identifiers before and after the text. I tried this, but it didn't work:
var = mystring.slice(/\"content\"\=\>\".\"/)
This should do the job
var = mystring[/"content"=>"(.*)"/, 1]
Note that:
.slice aliases []
none of the characters you escaped are special regexp characters where you're using them
you can "group" the bit you want to keep with ()
.slice / [] take a second parameter to pick a matched group
your_text = '"content"=>"The text I want to save to a variable"'
/"content"=>"(?<hooray>.*)"/ =~ your_text
Afterwards, hooray local variable will be magically set to contain your text. Can be used to set multiple variables.
This regex will match your string:
/\"content\"=>\"(.*)\"/
you can try rubular.com for testing
It looks like you're trying to truncate a sentence. You can split the sentence either on punctuation, or even on words.
mystring.split(".")
mystring.split("word")

how to convert strings like "this is an example" to "this-is-an-example" under ruby

How do I convert strings like "this is an example" to "this-is-an-example" under ruby?
The simplest version:
"this is an example".tr(" ", "-")
#=> "this-is-an-example"
You could also do something like this, which is slightly more robust and easier to extend by updating the regular expression:
"this is an example".gsub(/\s+/, "-")
#=> "this-is-an-example"
The above will replace all chunks of white space (any combination of multiple spaces, tabs, newlines) to a single dash.
See the String class reference for more details about the methods that can be used to manipulate strings in Ruby.
If you are trying to generate a string that can be used in a URL, you should also consider stripping other non-alphanumeric characters (especially the ones that have special meaning in URLs), or replacing them with an alphanumeric equivalent (example, as suggested by Rob Cameron in his answer).
If you are trying to make something that is a good URL slug, there are lots of ways to do it.
Generally, you want to remove everything that is not a letter or number, and then replace all whitespace characters with dashes.
So:
s = "this is an 'example'"
s = s.gsub(/\W+/, ' ').strip
s = s.gsub(/\s+/,'-')
At the end s will equal "this-is-an-example"
I used the source code from a ruby testing library called contest to get this particular way to do it.
If you're using Rails take a look at parameterize(), it does exactly what you're looking for:
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M001367
foo = "Hello, world!"
foo.parameterize => 'hello-world'

Not grab group match in Ruby Regex

I am trying to break down the following string:
"#command Text1 #command2 Text2"
in Ruby. I want to take out "Text1" and "Text2" in an array. To do this I am using the scan method and using this:
text.scan(/#* (.*?)(#|$)/)
However, when run, the script is pulling the # symbol in the middle as a separate match (presumably because the parenthesis are used in Ruby to indicate what string you want to pull out of the input):
Text1
#
Text2
My question is, how can I pull out Text1 and Text2 bearing in mind the expression needs to stop matching at both "#" and the end of a string?
If you want a non-capturing group use ?:
text.scan(/#* (.*?)(?:#|$)/)
As a sidenote, your regular expression looks like it might contain an error. Perhaps you meant this instead?
text.scan(/#\w+ (\w+)(?= #|$)/)
The difference is that your expression matches on " foo", which I guess is not intentional.
text.scan(/#* (.*?)(?:#|$)/)'
In your regex, you don't need the parentheses around '#|$'. The following will accomplish the same thing without the '#' being returned in a separate match group:
text.scan(/#* (.*?)[#\$]/)
Since you're looking only for a single character in that group, the square brackets will match any one character within them.
Here's how I'd do it:
text.scan(/#[^\s]* ([^#]*)/)
How does this regex look?
http://rubular.com/regexes/13264

Resources