How do you get Ruby to print the string that matches a certain regex
pattern.
I need to find a pattern where a small latin letter is has three large
letters on either side.
My code is:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str.scan(/([A-Z])\1{2}[a-z]\1{3}/)
print arr
The match is DDDbDDD but the arr prints only the first letter D.
How do I get it to print the full string (“DDDbDDD”)?
new_c
March 4, 2011, 9:13pm
2
2011/3/4 New C. [email protected] :
My code is:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str.scan(/([A-Z])\1{2}[a-z]\1{3}/)
print arr
The match is DDDbDDD but the arr prints only the first letter D.
How do I get it to print the full string (“DDDbDDD”)?
I don’t know why, but using [] rather than #scan does the work:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr
=> “DDDbDDD”
new_c
March 4, 2011, 9:15pm
3
2011/3/4 Iñaki Baz C. [email protected] :
I don’t know why, but using [] rather than #scan does the work:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr
Also it works using #scan if you check $& content:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str.scan(/([A-Z])\1{2}[a-z]\1{3}/)
print arr
=> “DDDbDDD”
new_c
March 4, 2011, 10:14pm
4
scan() returns an array of full matches–unless you have groups in your
regex. In that case, each element of the array is itself an array,
which contains the matches to the groups in your regex. Here is a
simple example:
str = “abcABC”
arr = str.scan /(…)(.)/
p arr
–output:–
[[“ab”, “c”], [“AB”, “C”]]
scan() found two matches for the regex, so their are two sub arrays.
Each of the sub arrays contains the matches for the groups in the regex.
Note that if parts of your regex are not part of a group, then they will
not appear in the sub arrays:
str = “abcABC”
arr = str.scan /(…)./
p arr
–output:–
[[“ab”], [“AB”]]
That is what you are seeing in your code.
new_c
March 4, 2011, 10:42pm
5
Thanks, I got it working now.
new_c
March 4, 2011, 10:31pm
6
Because you are interested in getting the whole match, create a group
for it:
str = “abcABC”
arr = str.scan /((…).)/
p arr
arr.each do |sub_arr|
puts sub_arr[0]
end
–output:–
[[“abc”, “ab”], [“ABC”, “AB”]]
abc
ABC
Note that your backreference becomes \2 rather than \1.
new_c
March 5, 2011, 12:14am
7
“Iñaki Baz C.” [email protected] wrote in post #985525:
2011/3/4 New C. [email protected] :
My code is:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str.scan(/([A-Z])\1{2}[a-z]\1{3}/)
print arr
The match is DDDbDDD but the arr prints only the first letter D.
How do I get it to print the full string (“DDDbDDD”)?
I don’t know why, but using [] rather than #scan does the work:
str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
arr = str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr
=> “DDDbDDD”
But…
puts arr.class
–output:–
String
The “[]” method of the String class only returns the first match, and
the method is defined to return the whole match. If you want one
of the groups instead, you indicate that with a group number, e.g.
puts str[ /(.)bc/, 1 ]
new_c
March 5, 2011, 1:16am
8
try this,
irb(main):001:0> str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
=> “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
irb(main):002:0> arr = str.scan(/([A-Z]{3}[a-z]{1}[A-Z]{3})/)
=> [[“DDDbDDD”]]
irb(main):009:0> puts arr[0]
DDDbDDD
=> nil
Thanks & Best Regards,
M. Karuppasamy
new_c
March 5, 2011, 5:20am
9
Karuppasamy M. wrote in post #985556:
try this,
irb(main):001:0> str = “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
=> “aaaDDDbDDDcDDDgHHyHjHJkkjUUh”
irb(main):002:0> arr = str.scan(/([A-Z]{3}[a-z]{1}[A-Z]{3})/)
=> [[“DDDbDDD”]]
irb(main):009:0> puts arr[0]
DDDbDDD
=> nil
Thanks & Best Regards,
M. Karuppasamy
Your regex drastically changes the meaning of the original regex. Your
regex will match “ABCaXYZ”; the original regex won’t.