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
Related
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!!!"
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.
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,'\"'))
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'd like to end up with the string "/a_setting/c\blah = something" where attribute gets evaluated to its value: blah. However, I'm seeing the following behavior where the preceding backslash seems to stop the evaluation of the variable:
attribute = "blah"
"/a_setting/c\#{attribute} = something"
=> "/a_setting/c\#{attribute} = something"
"/a_setting/c\ #{attribute} = something"
=> "/a_setting/c blah = something"
To get the string you want:
"/a_setting/c\\#{attribute} = something"
You need to escape the backslash by backslash.
When you do "\#", the "#" is escaped, and is interpreted not as an interpolation element, but as the verbatim "#", which in inspection, appears as "\#" in front of {...} to avoid ambiguity with interpolation.
When you do "\ ", the " " is (redundantly) escaped, and is interpreted as the verbatim " ".
I don't understand what you are pointing at.
But if you are trying to have your attributed evaluated in the string, probably this is what you want
"/a_setting/c\\#{attribute} = something"
coz by
"/a_setting/c\#{attribute} = something"
you are escaping the evaluation by #{} by adding the escape character \
So interpreter will evaluate #{} rather as an expression.
When you add another \ before the other \, the next \ is escaped and evaluated as a normal character.
"\#{attribute}" #:=> "\{attribute}
"\\#{attribute}" #;=> "\blah"
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