# Odd behavior of String#scan

**URL:** <https://www.ruby-forum.com/t/odd-behavior-of-string-scan/50856>\
**Category:** Ruby\
**Created:** [December 21, 2005, 10:35pm UTC](https://www.ruby-forum.com/t/odd-behavior-of-string-scan/50856 "2005-12-21T22:35:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![warren\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4bbf92/32.png) [@warren\_b](https://www.ruby-forum.com/u/warren_b)\
**Post date:** [December 21, 2005, 10:35pm UTC](https://www.ruby-forum.com/t/odd-behavior-of-string-scan/50856/1 "2005-12-21T22:35:40Z")

</div>

First off, the problem I am trying to solve can be simplified down  
to:

‘abcSTARTdef,ghi,jkl,ENDmno’.scan(/START([^,]\*,)\*END/)

```
What I want is [["def,"], ["ghi,"], ["jkl,"]] (or the same thing

```

without the commas), and I still need a way to achieve this. I can  
accomplish it with:

‘abcSTARTdef,ghi,jkl,ENDmno’.scan(/START(.\*)END/)[0][0].split(/,/)

```
But this does two operations where it seems like one should suffice.

```

Does someone know of a way to do this in a single operation?

```
Back to the odd behavior, the first expression actually returns

```

[[“jkl,”]]. I can’t figure out how that is the correct answer by any  
reasonable definition of “scan”. However, the equivalent String#match  
does the same kind of thing, so I must be missing something. Can  
someone please explain this behavior?

```
Thanks,

- Warren B.
```

---

<div class="post-metadata">

**Author:** ![warren\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4bbf92/32.png) [@warren\_b](https://www.ruby-forum.com/u/warren_b)\
**Post date:** [December 21, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/odd-behavior-of-string-scan/50856/2 "2005-12-21T22:50:47Z")

</div>

Warren B. wrote:

> ```
> But this does two operations where it seems like one should suffice.
> 
> ```
> 
> Does someone know of a way to do this in a single operation?

I would suggest:

‘abcSTARTdef,ghi,jkl,ENDmno’.match(/START(.\*)END/)[1].split(’,’)

I don’t know of a way to accomplish it in one step. String#scan attempts  
to match the whole regex at multiple places within the string; but you  
need the START and END to delimit the substring over which scan  
operates.

> ```
> Back to the odd behavior, the first expression actually returns
> 
> ```
> 
> [[“jkl,”]]. I can’t figure out how that is the correct answer by any  
> reasonable definition of “scan”. However, the equivalent String#match  
> does the same kind of thing, so I must be missing something. Can  
> someone please explain this behavior?

Because you have this:

([^,]_,)_

The final \* allows the group to match multiple times. The MatchData will  
hold only the last match however, which is “jkl,”.

---

<div class="post-metadata">

**Author:** ![warren\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4bbf92/32.png) [@warren\_b](https://www.ruby-forum.com/u/warren_b)\
**Post date:** [December 21, 2005, 11:32pm UTC](https://www.ruby-forum.com/t/odd-behavior-of-string-scan/50856/3 "2005-12-21T23:32:56Z")

</div>

Will this do?

s=“abcSTARTdef,ghi,jkl,ENDmno”  
s.scan(/START(.\*)END/).to\_s.split(",")

=\> [“def”, “ghi”, “jkl”]
