up vote 0 down vote favorite
share [fb]

Trying to grab the two $ values and the X value from this string in Ruby/watir:

16.67%: $xxx.xx down, includes the Policy Fee, and x installments of $xxx.xx

So far I've got:

16.67%:\s+\$(\d+.\d{2}) 

which grabs the first xxx.xx fine, what do I need to add to it to grab the last two variables and load this all into an array?

link|improve this question

50% accept rate
feedback

5 Answers

up vote 0 down vote accepted

You can use the following, but regex may be unnecessary if the surrounding text is always the same:

\$(\d+.\d{2}).*?(\d+) installments.*?\$(\d+.\d{2})

http://www.rubular.com/r/sk5wO3fyZF

link|improve this answer
myarray = "16.67%: $xxx.xx down, includes the Policy Fee, and x installments of $xxx.xx".scan /\$\d+\.\d{2}/ – Dave McNulla Mar 16 at 19:35
feedback

/\$(\d+.\d{2}).+\$(\d+.\d{2})/ should do it. it wont matter what text is there, only that there are two "$" in the sentence.

link|improve this answer
feedback

Have you considered just splitting the string on the $ character?, then manipulating what you get with a regex or basic string commands?

link|improve this answer
Well I dont just need the two $ I need the number X before installments as well.. – SamuraiJack Mar 18 at 14:13
feedback

You better use scan.

sub(/.*%/, '').scan(/\$?([\d\.]+)/)
link|improve this answer
feedback

if you know that the text in between will always be the same you could just:

16.67%:\s+\$(\d+.\d{2}) down, includes the Policy Fee, and x installments of (\d+.\d{2})
link|improve this answer
only difference may be that the term Policy Fee may change.. thanks... – SamuraiJack Mar 16 at 16:40
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.