Re: [Rails] Problem with regexp split
On Mar 1, 2010, at 9:50 AM, gerry.jenkins@gmail.com wrote:
> I am trying to split some text into an array seperated by one or more
> <br>
>
> Here is some test code:
>
> s = "one<br>two<br><br>three<br><br><br>four"
> p s.split(/(<br>)+/);
>
> it should split into ["one","two","three","four"] because the /
> (<br>)+/ pattern should use one or more <br> as the pattern to split
> around
>
> but it does this
> ["one", "<br>", "two", "<br>", "three"]
>
> Why does it do this and what split could I use to get it to work?
>
> Note:, I know that I could just fix it by removeing the <br> lines
> after it is done from the array, but it seems that the regular
> expression in split should work.
Interesting. Docs say:
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.
Which seems to be saying exactly what you are are describing. If a
regexp is used the match isn't "eaten", but simply divided on.
You could split it on "<br>" and then remove any blank elements... not
sure if that's any better than your alternative approach though.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home