From: Phrogz Date: 2007-09-19T07:45:04+09:00 Subject: Re: Regexp help On Sep 18, 4:04 pm, Jeremy Woertink 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: > > > irb> 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: > > irb> %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: > > irb> 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 > > C:\rails_apps\hotswipe>irb > irb(main):001:0> require 'config/environment' > => true > irb(main):002:0> encoding = ";6277200301500269=?" > => ";6277200301500269=?" > irb(main):003:0> encoding =~ /(;.*\?)|(\?.*;)/ > => 0 > irb(main):004:0> > > So what does the 0 mean? 0 matches? If all else fails, I will use the > ugly version :) 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.)