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.
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:
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:
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"