I'm working on some text processing in Ruby 1.8.7 to support some custom shortcodes that I've created. Here are some examples of my shortcode:

```
[CODE first-part]
[CODE first-part second-part]

```

I'm using the following RegEx to grab the 

```
text.gsub!( /\[CODE (\S+)\s?(\S?)\]/i, replacementText )

```

The problem is this: the regex doesn't work on the following text:

```
[CODE first-part][CODE first-part-again]

```

The results are as follows:

```
1.  first-part][CODE
2.  first-part-again

```

It seems that the `\s?` is the problematic part of the regex that is searching on until it hits the last space, not the first one. When I change the regex to the following:

```
\[CODE ([\w-]+)\s?(\S*)\]/i

```

It works fine. The only concern I have is what all `\w` vs `\s` as I want to make sure the `\w` will match URL-safe characters.

I'm sure there's a perfectly valid explanation, but it's eluding me. Any ideas? Thanks!