# Regular Expression pattern matching question

**URL:** <https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458>\
**Category:** Rails\
**Created:** [September 25, 2008, 1:21am UTC](https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458 "2008-09-25T01:21:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 25, 2008, 1:21am UTC](https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458/1 "2008-09-25T01:21:36Z")

</div>

Hi there, I’m trying to split a string using RegExp to extract a price  
from it. The price may or may not have a dollar sign in front of it  
and may or may not contain a decimal value. Below are some string  
examples of what the regular expression would need to handle to split  
out the price:

Example 1:  
“2 starbuck’s coffees 9.99” \<- price is 9.99

Example 2:  
“99 red baloons 175” \<- price is 175

Example 3:  
“starbuck’s coffee $9.99” \<- price is 9.99

Example 4:  
“starbucks coffee 9” \<- price is 9

So the trouble is: a) in Example 3, I can’t count on the dollar sign  
being there all the time. Some instances it will, other instances it  
won’t and b) in the case of Example 2, there may be a number at the  
start of the string of indeterminate length that I want to ignore  
since this could represent the quantity, not the price.

What would the RegExp look like that would split out the price?

Thanks in advance.  
-A

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [October 6, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458/2 "2008-10-06T23:14:25Z")

</div>

eg /(\d+.){0,1}\d+$/

g jo

Am Mittwoch, den 24.09.2008, 16:20 -0700 schrieb ressister:

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [October 7, 2008, 12:33am UTC](https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458/3 "2008-10-07T00:33:01Z")

</div>

you can always use [http://www.rubular.com/](http://www.rubular.com/) to test your RexExp.

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [October 7, 2008, 11:59am UTC](https://www.ruby-forum.com/t/regular-expression-pattern-matching-question/149458/4 "2008-10-07T11:59:14Z")

</div>

Am Montag, den 06.10.2008, 15:14 -0700 schrieb Dejan D.:

> you can always use [http://www.rubular.com/](http://www.rubular.com/) to test your RexExp.  
> This Website does not work correctly.

g jo
