Introducing backslash and quote at beginning of string [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 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,'\"'))

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.

Why is $1 not being set by my regular expression match? [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 have the code as below:
require 'colored'
require 'byebug'
str = '英 [faɪnd] 美 [faɪnd]'
regex = /\[([^\[\]]*)\]/
blk = Proc.new{|mat| mat.send(:yellow)}
to_search = str.dup
while regex =~ to_search do
byebug
str.sub! /#{$1}/, blk.call($1)
...
end
Before str.sub! /#{$1}/, blk.call($1),
$1 is "faɪnd"
$' is " 美 [faɪnd]"
After it,
$1 is nil
$' is "] 美 [faɪnd]"
Why does this happen?
It is because the regex /#{$1}/ becomes /faɪnd/. When this regex matches against '英 [faɪnd] 美 [faɪnd]',
The first capture $1 will be nil because /faɪnd/ has no capturing group.
The affix $' becomes "] 美 [faɪnd]", which is right after the match.
It's unclear how you think $1 is being set. It's not a positional parameter like in Bash; it's a special global variable described in the Regexp class. $1 is set to the first capture group of the last match.
Consider:
"foo".match /(foo)/; $1
#=> "foo"
"foo".match /foo/; $1
#=> nil

Regular expression for series of numbers 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 8 years ago.
Improve this question
I am trying to match a pattern as given below
pattern: file.update.20120304.xml
file.update.1.xml
file.update.201203040002.xml
If i have to match the pattern leaving the numbers file.update..xml
how can I do in ruby?
"file.update.20120304.xml"[/\d+/] # => "20120304"
"file.update.1.xml"[/\d+/] # => "1"
"file.update.201203040002.xml"[/\d+/] # => "201203040002"
You can use Regexp and gsub to extract the numbers
filename = 'file.update.20120304.xml'
numbers = filename.gsub(/\Afile\.update\.(\d+)\.xml\z/, '\1')
The used regexp is composed as following:
a first part file\.update\. to be sure you have "file.update"
a middle part ([0-9]+) to have one or more number
an end part \.xml to be sure to have ".xml" at the end
the anchors \A and \z for the beginning and the end of the string
The middle part is wrapped with () to be a Regexp variable that you can reuse in the replacement string as \1
Update with variableName[/regular expression/]
filename = 'file.update.20120304.xml'
numbers = filename[/\Afile\.update\.(\d+)\.xml\z/, 1]
It returns the first captured group, i.e. between ().
p "file.update.201203040002.xml".split(".") .map { |x| x if x.to_i != 0}.compact[0].to_i
#=> 201203040002
p "file.update.20120304.xml".split(".") .map { |x| x if x.to_i != 0}.compact[0].to_i
#=> 20120304

How can I do the following regex 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 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 +"

Resources