Regexp help

J

Jeremy Woertink

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
 
D

Daniel Lucraft

Jeremy said:
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
 
J

Jeremy Woertink

Daniel said:
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

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
 
P

Phrogz

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.)
 
J

Jeremy Woertink

Gavin said:
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
 
J

Jeremy Woertink

Jeremy said:
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

haha, ok you can disregard that, I looked under string and seen what you
were talking about. My bad.

thanks

~Jeremy
 
P

Peña, Botp

RnJvbTogSmVyZW15IFdvZXJ0aW5rIFttYWlsdG86amVyZW15d29lcnRpbmtAZ21haWwuY29tXSAN
CiMgV2hhdCBJIHdhbnQgdG8gZG8gaXMgZGV0ZXJtaW5lIGlmIGEgc3RyaW5nIGluY2x1ZGVzIDIg
c3BlY2lhbCANCiMgY2hhcmFjdGVycyBhIHNlbWktY29sb24gYW5kIGEgcXVlc3Rpb24gbWFyay4N
CiMgaXJiKG1haW4pOjAwOTowPiBlbmNvZGluZyA9ICI7NjI3NzIwMDMwMTUwMDI2OT0/Ig0KIyA9
PiAiOzYyNzcyMDAzMDE1MDAyNjk9PyINCiMgaXJiKG1haW4pOjAxMDowPiBlbmNvZGluZy5pbmNs
dWRlPyglcns7XD99LnRvX3MpDQojID0+IGZhbHNlDQoNCnN0YXJ0IHNpbXBsZSwNCg0KY2hlY2sg
aWYgYSBzdHJpbmcgY29udGFpbnMgYSA7IG9yID8NCg0KaXJiKG1haW4pOjA5NTowPiBlbmNvZGlu
Z1svO3xcPy9dDQo9PiAiOyINCg0KY2hlY2sgaWYgYSBzdHJpbmcgc3RhcnRzIHcgYSA7IGFuZCBl
bmRzIHdpdGggYSA/DQoNCmlyYihtYWluKTowOTY6MD4gZW5jb2RpbmdbL147LipcPyQvXQ0KPT4g
Ijs2Mjc3MjAwMzAxNTAwMjY5PT8iDQoNCnN0cmluZ1tyZWdleF0gcmV0dXJucyB0aGUgbWF0Y2hl
ZCAoc3ViKXN0cmluZyAod2MgaXMgdHJ1ZSBpbiBydWJ5KQ0Kb3RoZXJ3aXNlIGl0IHJldHVybnMg
bmlsICh3YyBpcyBmYWxzZSBpbiBydWJ5KQ0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
A

Ari Brown

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top