How can I do the following regex in ruby? [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how do i match each of the following with regex ?
some text includes [any number of character]. need to pull the entire [asdfasdf]
#tableid='sometext' I need to pull the sometext
mary and drugs I need to pull " and ", spaces included.

irb(main):002:0> "some text include [abcdef]".match(/\[(.*)\]/)[1]
=> "abcdef"
irb(main):005:0> "#table_id='2356'".match(/'(.*)'/)[1]
=> "2356"
irb(main):006:0> "mary and drugs".match(/mary(.*)drugs/)[1]
=> " and "

Try these:
"\[(.*?)\]"
"#\w+='(.*?)'"
" +and +"

Related

Move all instances of a character to the end of the string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
If I havd a string like:
"Hi! Hi! Hi!"
How can I move the exclamation marks to the end of the string.
Expected output:
"Hi Hi Hi!!!"
You could do it with:
s = "Hi! Hi! Hi!"
s1 = s.delete("!")+s.scan("!").join
.delete("!") just returns a string without "!".
.scan("!") collects all "!" from a string.
.join just joins all elements of an array to a string
Just for fun, you could sort the characters by their index, except when the character is ! :
"Hi! Hi! Hi!".each_char.sort_by.with_index{ |c, i| c == '!' ? Float::INFINITY : i }.join
#=> "Hi Hi Hi!!!"
Another way:
str.tr('!','') + '!'*str.count('!')
#=> "Hi Hi Hi!!!"

How to interpolate a regex into string and use that string as regex to compare in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider i have a strings
goo = "test check\ncode"
if goo =~ /#{Regexp.quote(foo)}/
puts "success!"
end
I need to compare with "foo" regex. How can i write this regex?
Kindly help me in to find this!
I assume that foo contains a string, which represents the regexp. If so, you can initialize Regexp object from that string and perform your matching as follows:
foo = 'test.*check.*code'
goo =~ Regexp.new(foo, Regexp::MULTILINE)
goo = "test check\ncode"
foo = "test.*check.*code"
goo =~ /#{foo}/m
#⇒ 0
The reason why your regexp did not do the trick, is that you have to explicitly set . to match new lines with m Regexp modifier.

Replace special characters in string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have strings like
"Ruby & Rails"
"Ruby& Rails"
"Ruby !Rails"
I want to convert them to "Ruby-Rails". How can I do this?
Assuming 1,2,3 are serial number for example and not are actually present in String, you can use split and join:
a = "Ruby & Rails"
a.split(/\W+/).join("-")
# => "Ruby-Rails"
"Ruby& Rails".split(/\W+/).join("-")
# => "Ruby-Rails"
"Ruby !Rails".split(/\W+/).join("-")
# => "Ruby-Rails"
Alternatively if serial number is also part of input string:
"1. Ruby& Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"
"2. Ruby& Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"
"3. Ruby !Rails".split(/\W+|\d+/).reject(&:empty?).join("-")
# => "Ruby-Rails"
You could use gsub.
string.gsub(/\s*\W+\s*/, "-")
OR
string.gsub(/\W+/, "-")
" Ruby ! Rails ".split(/\W+/).reject(&:empty?).join("-")
=> "Ruby-Rails"

Introducing backslash and quote at beginning of string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to convert "her" to "\"her".
I've tried using insert method:
>> "her".insert(0,'\"')
=> "\\"her"
and
>> "her".insert(0,'"')
=> ""her"
None of them gives me what I want: "\"her"
"her".insert(0,'"')
actually returns "\"her", which is what you said you wanted in the first place.
If you want to obtain "\"her\"", you might want to use Object#inspect:
"her".inspect
=> "\"her\""
Or, you can simply concatenate quotes at the beginning and at the end:
'"' + "her" + '"'
=> "\"her\""
If you just want "\"her"
a = "her".insert(0,'\"')
#=> "\\\"her"
puts a
#=> \"her
If you want output "\"her\""
a = "her"
#=> "her"
b = '\"'+a+'\"'
#=> "\\\"her\\\""
puts b
#=> \"her\"
I think your code is fine, you just don't know the meaning of \\ in console.
The first \ is escape character and second \ is character itself.
You will see \"her in the text foo.txt as you expected by:
File.write("foo.txt", "her".insert(0,'\"'))

Ruby regex with variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to have a regular expression that works with a variable in it
Right now it looks like var=status.text[/.*#{keyword}.*is now available.*/io]
but the variable breaks the regular expression
What do you mean by 'breaks'?
It works out of the box
[36] pry(main)> a = 1
=> 1
[37] pry(main)> /#{a}/
=> /1/
You can put the variable in a string first, then convert the string to a regexp as follows:
[4] pry(main)> keyword = "cat"
=> "cat"
[5] pry(main)> my_regexp = Regexp.new(".*#{keyword}.*is now available.*")
=> /.*cat.*is now available.*/

Resources