# One more regular expression, please

**URL:** <https://www.ruby-forum.com/t/one-more-regular-expression-please/179640>\
**Category:** Ruby\
**Created:** [December 1, 2009, 3:07am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640 "2009-12-01T03:07:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 3:07am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/1 "2009-12-01T03:07:24Z")

</div>

Hi,

I really can’t figure out how regular expressions work, and since I  
don’t use them frequently is easier to ask…

I have the follwow string

++((100))++askjasdjksjdkasdkjk++((200))++skdjsjd

I need a reg expression that extracts only the first number inside the  
first ++(( ))++ and ignore the rest…, in this case I need to return  
100

thanks

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 3:58am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/2 "2009-12-01T03:58:51Z")

</div>

On Monday 30 November 2009 08:07:26 pm J. mp wrote:

> I have the follwow string
> 
> ++((100))++askjasdjksjdkasdkjk++((200))++skdjsjd
> 
> I need a reg expression that extracts only the first number inside the  
> first ++(( ))++ and ignore the rest…, in this case I need to return  
> 100

Since I can’t figure out why you’d ever want to do that in a real app, I  
have  
to ask: Is this a homework assignment?

Well, I’d rather not assume, so I’ll give you a partial answer: Regular  
expressions don’t return things, they match things. You can’t write a  
regular  
expression (that I know of) which will match only what you’re asking  
for,  
unless you assume things about that specific string. Especially in  
homework,  
assuming things like that is a Bad Idea.

There is a solution, and it includes regular expressions, but they are  
not the  
whole story.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 4:05am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/3 "2009-12-01T04:05:06Z")

</div>

J. mp wrote:

> Hi,
> 
> I really can’t figure out how regular expressions work,

So you’re giving up and expecting us to do your work for you? Come on!  
You could at least try.

> and since I  
> don’t use them frequently is easier to ask…

Yes, ask. Ask irb. Ask [http://www.rubular.com](http://www.rubular.com) . But don’t you dare ask  
this list until you’ve made an attempt. Begging others to write code  
because you’re too lazy to try is not cool!

> I have the follwow string
> 
> ++((100))++askjasdjksjdkasdkjk++((200))++skdjsjd
> 
> I need a reg expression that extracts only the first number inside the  
> first ++(( ))++ and ignore the rest…, in this case I need to return  
> 100
> 
> thanks

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 6:40am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/4 "2009-12-01T06:40:33Z")

</div>

J. mp wrote:

> you’re both right. That was a dumb question. I will try to find it by  
> myself. Thank you.

Good luck. If you get stuck, help is available. Regexes are lots of  
fun once you understand them!

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 8:36am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/5 "2009-12-01T08:36:51Z")

</div>

/[^+]_++((([0-9]+)))++._/

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 4:23am UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/6 "2009-12-01T04:23:08Z")

</div>

you’re both right. That was a dumb question. I will try to find it by  
myself. Thank you.

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/7 "2009-12-01T14:48:10Z")

</div>

Ruby Bucket wrote:

> /[^+]_++((([0-9]+)))++._/

Not quite. Anyway, don’t spoon-feed the lazy. 🙂

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![j-merrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@j-merrill](https://www.ruby-forum.com/u/j-merrill)\
**Post date:** [December 1, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/one-more-regular-expression-please/179640/8 "2009-12-01T14:47:55Z")

</div>

extractor = Regexp.new(’++(((\d+)))++’)  
extractor.match will extract all the ++(123)++ like parts and extract  
all  
123s into an array,  
if the string start with ++, try  
^++(((\d+)))++
