Can't understand String#split's behavior

Å

冷雨

[Note: parts of this message were removed to make it a legal post.]

When I use ruby 1.8.7 I have a problem with String#split.

It's fine:["b", "b", "b", "b"]

But it cant work:["b", "A", "b", "A", "b", "A", "b"]

I think that /A/ and /(A)/ are almost equivalent, but why can't I use /(A)/
to split string?

-Lai
 
B

botp

I think that /A/ and /(A)/ are almost equivalent,
almost

but why can't I use /(A)/ to split string?

as documented, you can, but w a different behaviour/result for split


(from ruby core)
=3D=3D=3D Implementation from String
---------------------------------------------------------------------------=
---
str.split(pattern=3D$;, [limit]) -> anArray

---------------------------------------------------------------------------=
---

Divides str into substrings based on a delimiter, returning an array of
these substrings.

If pattern is a String, then its contents are used as the
delimiter when splitting str. If pattern is a single
space, str is split on whitespace, with leading whitespace and runs of
contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern
matches. Whenever the pattern matches a zero-length string, str is split
into individual characters. If pattern contains groups, the
respective matches will be returned in the array as well.
....
 
Å

冷雨

Sorry for my careless.
And thank you very match.


I think that /A/ and /(A)/ are almost equivalent,
almost

but why can't I use /(A)/ to split string?

as documented, you can, but w a different behaviour/result for split


(from ruby core)
=3D=3D=3D Implementation from String

-------------------------------------------------------------------------= -----
str.split(pattern=3D$;, [limit]) -> anArray


-------------------------------------------------------------------------= -----

Divides str into substrings based on a delimiter, returning an array of
these substrings.

If pattern is a String, then its contents are used as the
delimiter when splitting str. If pattern is a single
space, str is split on whitespace, with leading whitespace and runs of
contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern
matches. Whenever the pattern matches a zero-length string, str is split
into individual characters. If pattern contains groups, the
respective matches will be returned in the array as well.
....
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top