Printing regex results

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")?

···

--
Posted via http://www.ruby-forum.com/.

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"

···

2011/3/4 New C. <coding25@yahoo.com>:

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ñaki Baz Castillo
<ibc@aliax.net>

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.

···

--
Posted via http://www.ruby-forum.com/.

scan() returns an array of full matches--unless you have groups in your
regex. In that case, scan() returns an array where each element is
itself an array, and each sub array 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.

···

--
Posted via http://www.ruby-forum.com/.

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 then becomes \2 rather than \1.

···

--
Posted via http://www.ruby-forum.com/.

Thanks, I got it working now.

···

--
Posted via http://www.ruby-forum.com/.

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"

···

2011/3/4 Iñaki Baz Castillo <ibc@aliax.net>:

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

--
Iñaki Baz Castillo
<ibc@aliax.net>

"Iñaki Baz Castillo" <ibc@aliax.net> wrote in post #985525:

···

2011/3/4 New C. <coding25@yahoo.com>:

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

--
Posted via http://www.ruby-forum.com/\.

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

···

On Sat, Mar 5, 2011 at 4:46 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

"Iñaki Baz Castillo" <ibc@aliax.net> wrote in post #985525:
> 2011/3/4 New C. <coding25@yahoo.com>:
>> 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

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/\.