K, so I suck at regular expressions. I just don’t seem to understand
them.
What I want to do is determine if a string includes 2 special characters
a semi-colon and a question mark.
Here’s my example that I’m trying to work with.
irb(main):009:0> encoding = “;6277200301500269=?”
=> “;6277200301500269=?”
irb(main):010:0> encoding.include?(%r{;?}.to_s)
=> false
irb(main):011:0>
I want this to return true, but it doesn’t
Can anyone help me out?
thanks,
~Jeremy
Jeremy W. 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
On Sep 18, 4:04 pm, Jeremy W. [email protected] wrote:
On the other hand, if you want to use regular expressions regardless,
is equivalent to
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.)
Daniel L. wrote:
Jeremy W. 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 
thanks,
~Jeremy
Gavin K. wrote:
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?
~Jeremy
Jeremy W. wrote:
Gavin K. wrote:
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.
thanks
~Jeremy
From: Jeremy W. [mailto:[email protected]]
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
start simple,
check if a string contains a ; or ?
irb(main):095:0> encoding[/;|?/]
=> “;”
check if a string starts w a ; and ends with a ?
irb(main):096:0> encoding[/^;.*?$/]
=> “;6277200301500269=?”
string[regex] returns the matched (sub)string (wc is true in ruby)
otherwise it returns nil (wc is false in ruby)
kind regards -botp
If it’s going to be going heavy on the regexp… (I have no shame)
Try TextualRegexp. Seriously, it was written so you DON"T have to
hurt your brain over it.
~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.