Peege151
Peege151

Reputation: 1560

Regexp for certain character to end of line

I have a string

"So on and so forth $5.99"  

I would like to extract everything after the $ until the end of the line.

/$ finds the character $. How do I select the rest of the string? I know it's something \z but I can't get the syntax right.

Upvotes: 0

Views: 62

Answers (3)

hwnd
hwnd

Reputation: 70732

No, /$ does not match that character. You need to escape it \ to match a literal.

string = "So on and so forth $5.99"
result = string.match(/\$(.*)$/)
puts result[1] #=> "5.99"

Upvotes: 2

John C
John C

Reputation: 4396

In regexp $ represents the end of the line. So in your case you need \$.*$ To include your escaped $ and everything (.*) up until the end of the line $.

Upvotes: 3

Michael Bleigh
Michael Bleigh

Reputation: 26313

If you want to capture everything after the $, you'll want:

/\$(.*)\z/

See http://rubular.com/r/T4fR1SEl3j

Upvotes: 1

Related Questions