printing regex results

N

New C.

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

Iñaki Baz Castillo

2011/3/4 New C. said:
My code is:

str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D 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 =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr

=3D> "DDDbDDD"


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
I

Iñaki Baz Castillo

2011/3/4 I=C3=B1aki Baz Castillo said:
I don't know why, but using [] rather than #scan does the work:

str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr

Also it works using #scan if you check $& content:

str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D str.scan(/([A-Z])\1{2}[a-z]\1{3}/)
print arr

=3D> "DDDbDDD"

--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
7

7stud --

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

7stud --

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

7stud --

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

7stud --

I=C3=B1aki Baz Castillo said:
2011/3/4 New C. said:
My code is:

str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D 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 =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr

=3D> "DDDbDDD"

...but:

puts arr.class

--output:--
String

-- =

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

Karuppasamy M

try this,

irb(main):001:0> str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
=3D> "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
irb(main):002:0> arr =3D str.scan(/([A-Z]{3}[a-z]{1}[A-Z]{3})/)
=3D> [["DDDbDDD"]]
irb(main):009:0> puts arr[0]
DDDbDDD
=3D> nil

Thanks & Best Regards,
M. Karuppasamy



I=C3=B1aki Baz Castillo said:
2011/3/4 New C. said:
My code is:

str =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D 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 =3D "aaaDDDbDDDcDDDgHHyHjHJkkjUUh"
arr =3D str[ /([A-Z])\1{2}([a-z])\1{3}/ ]
print arr

=3D> "DDDbDDD"

...but:

puts arr.class

--output:--
String
 
7

7stud --

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top