# What I want to do is determine if a string includes 2 special
# characters a semi-colon and a question mark.
# irb(main):009:0> encoding = ";6277200301500269=?"
# => ";6277200301500269=?"
# irb(main):010:0> encoding.include?(%r{;\?}.to_s)
# => false
It means that it found the first match starting at index 0 in the
string. (Try reading the documentation on the =~ method.)
Note that the supplied regexp will fail if your string has a newline
in it. You may want to append the 'm' (multiline) modifier to it, e.g.
/;.*\?|\?.*;/m
(There's also no need for the parentheses, and a minor theoretical
speed/memory reason to leave them off.)
···
On Sep 18, 4:04 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote:
Daniel Lucraft wrote:
> Jeremy Woertink wrote:
>> What I want to do is determine if a string includes 2 special characters
>> a semi-colon and a question mark.
> You don't need regular expressions to do that, try this instead:
> > encoding.include? ";" and encoding.include? "?"
> => true
> On the other hand, if you want to use regular expressions regardless,
> you should notice that String#include? takes a string rather than a
> Regexp.
> Note that:
> > %r{;\?}.to_s
> > "(?-mix:;\\?)"
> So
> encoding.include?(%r{;\?}.to_s)
> is equivalent to
> encoding.include?("(?-mix:;\\?)")
> which is not going to succeed.
> To use a regexp, try:
> > encoding =~ /(;.*\?)|(\?.*;)/
> => true.
> best
> Dan
Thanks for the help. I would like to use a regexp becuase I think it
will look nicer in the code. I tried your example and got
It means that it found the first match starting at index 0 in the
string. (Try reading the documentation on the =~ method.)
ok, that makes sense. I did check out the docs on http://www.ruby-doc.org/core/
I clicked the =~ method under Regexp and it sends me to
rxp.match(str) => matchdata or nil
Returns a MatchData object describing the match, or nil if there was no
match. This is equivalent to retrieving the value of the special
variable $~ following a normal match.
which didn't say anything about indexes. Are there any other sites that
have different documentation?
It means that it found the first match starting at index 0 in the
string. (Try reading the documentation on the =~ method.)
ok, that makes sense. I did check out the docs on RDoc Documentation
I clicked the =~ method under Regexp and it sends me to
rxp.match(str) => matchdata or nil
Returns a MatchData object describing the match, or nil if there was no
match. This is equivalent to retrieving the value of the special
variable $~ following a normal match.
which didn't say anything about indexes. Are there any other sites that
have different documentation?
~Jeremy
haha, ok you can disregard that, I looked under string and seen what you
were talking about. My bad.