How about:
str = "foobar"
if (str =~ /^foo/)
...
end
if (str =~ /bar$/)
...
end
The real solution which allows you to use arbitrary strings would have to be more like
str =~ /#{Regexp::quote("a string...")}$/
and I don't think that's very idiomatic.
You don't have to use
Regexp::quote(...):
start_with = "foo"
end_with = "baz"
end_with2 = "bar baz"
str = "foo bar baz"
puts "#{str} starts with '#{start_with}'" if str =~ /^#{start_with}/
puts "#{str} starts with 'foo bar'" if str =~ /^foo bar/
puts "#{str} ends with '#{end_with}'" if str =~ /#{end_with}$/
puts "#{str} ends with '#{end_with2}'" if str =~ /#{end_with2}$/
puts "#{str} ends with 'bar baz'" if str =~ /bar baz$/
This will produce:
foo bar baz starts with 'foo'
foo bar baz starts with 'foo bar'
foo bar baz ends with 'baz'
foo bar baz ends with 'bar baz'
foo bar baz ends with 'bar baz'
You can even check start_with and end_with in one shot by something like:
str =~ /^foo.*?baz$/
start_with = "*** make money fast ***"
str = "Hello World!"
puts "#{str} starts with '#{start_with}'" if str =~ /^#{start_with}/
produces this error for me:
RegexpError: invalid regular expression; there's no previous pattern, to which '*' would define cardinality at 2: /^*** make money fast ***/
So the Regexp::quote() is needed.
Bummer...how did I miss that? I guess I never had a single case where the string contained a special character...but hey, it's not a bad thing that I learned something from somebody.
I wish I could change my previous vote to a YES, but (surprise!) the system won't allow me to re-vote/change...sorry about that... :-(
Heh, that's no problem. We have 4 out of 5 positives and I guess matz actually reads the comments. :)
perhaps starts_with is a better name that start_with
see
That might be the case. -- I was just choosing start_with? because I wanted to stay consistent with some original methods. (mainly Array#include?)
Re: String#start_with? / #end_with? (, 2003-08-21 22:26:43)
str = "foobar"
if (str =~ /^foo/)
...
end
if (str =~ /bar$/)
...
end
It's not as easy as that... (, 2003-08-22 00:43:44)
str =~ /#{Regexp::quote("a string...")}$/
and I don't think that's very idiomatic.Re: It's not as easy as that... (, 2003-08-22 11:13:22)
start_with = "foo"
end_with = "baz"
end_with2 = "bar baz"
str = "foo bar baz"
puts "#{str} starts with '#{start_with}'" if str =~ /^#{start_with}/
puts "#{str} starts with 'foo bar'" if str =~ /^foo bar/
puts "#{str} ends with '#{end_with}'" if str =~ /#{end_with}$/
puts "#{str} ends with '#{end_with2}'" if str =~ /#{end_with2}$/
puts "#{str} ends with 'bar baz'" if str =~ /bar baz$/
This will produce:
foo bar baz starts with 'foo'
foo bar baz starts with 'foo bar'
foo bar baz ends with 'baz'
foo bar baz ends with 'bar baz'
foo bar baz ends with 'bar baz'
You can even check start_with and end_with in one shot by something like:
str =~ /^foo.*?baz$/
Re: It's not as easy as that... (, 2003-08-22 19:02:00)
produces this error for me:
RegexpError: invalid regular expression; there's no previous pattern, to which '*' would define cardinality at 2: /^*** make money fast ***/
So the Regexp::quote() is needed.
Re: It's not as easy as that... (, 2003-08-22 22:50:46)
I wish I could change my previous vote to a YES, but (surprise!) the system won't allow me to re-vote/change...sorry about that... :-(
Re: It's not as easy as that... (, 2003-08-24 00:59:14)
Re: String#start_with? / #end_with? (, 2003-09-02 19:50:23)
see
Re: String#start_with? / #end_with? (, 2003-09-05 10:00:08)