Regexp help : RUBY
This is a discussion on Regexp help within the RUBY forums in Programming Languages category; 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 -- Posted via http://www.ruby-forum.com/ ....
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| 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 -- Posted via http://www.ruby-forum.com/. |
|
#2
| |||
| |||
| 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 -- Posted via http://www.ruby-forum.com/. |
|
#3
| |||
| |||
| 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 ![]() thanks, ~Jeremy -- Posted via http://www.ruby-forum.com/. |
|
#4
| |||
| |||
| 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: > > > 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.) |
|
#5
| |||
| |||
| Gavin Kistner 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 -- Posted via http://www.ruby-forum.com/. |
|
#6
| |||
| |||
| Jeremy Woertink wrote: > Gavin Kistner 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 haha, ok you can disregard that, I looked under string and seen what you were talking about. My bad. thanks ~Jeremy -- Posted via http://www.ruby-forum.com/. |
|
#7
| |||
| |||
| RnJvbTogSmVyZW15IFdvZXJ0aW5rIFttYWlsdG86amVyZW15d29lcnRpbmtAZ21haWwuY29tXSAN CiMgV2hhdCBJIHdhbnQgdG8gZG8gaXMgZGV0ZXJtaW5lIGlmIGEgc3RyaW5nIGluY2x1ZGVzIDIg c3BlY2lhbCANCiMgY2hhcmFjdGVycyBhIHNlbWktY29sb24gYW5kIGEgcXVlc3Rpb24gbWFyay4N CiMgaXJiKG1haW4pOjAwOTowPiBlbmNvZGluZyA9ICI7NjI3NzIwMDMwMTUwMDI2OT0/Ig0KIyA9 PiAiOzYyNzcyMDAzMDE1MDAyNjk9PyINCiMgaXJiKG1haW4pOjAxMDowPiBlbmNvZGluZy5pbmNs dWRlPyglcns7XD99LnRvX3MpDQojID0+IGZhbHNlDQoNCnN0YXJ0IHNpbXBsZSwNCg0KY2hlY2sg aWYgYSBzdHJpbmcgY29udGFpbnMgYSA7IG9yID8NCg0KaXJiKG1haW4pOjA5NTowPiBlbmNvZGlu Z1svO3xcPy9dDQo9PiAiOyINCg0KY2hlY2sgaWYgYSBzdHJpbmcgc3RhcnRzIHcgYSA7IGFuZCBl bmRzIHdpdGggYSA/DQoNCmlyYihtYWluKTowOTY6MD4gZW5jb2RpbmdbL147LipcPyQvXQ0KPT4g Ijs2Mjc3MjAwMzAxNTAwMjY5PT8iDQoNCnN0cmluZ1tyZWdleF0gcmV0dXJucyB0aGUgbWF0Y2hl ZCAoc3ViKXN0cmluZyAod2MgaXMgdHJ1ZSBpbiBydWJ5KQ0Kb3RoZXJ3aXNlIGl0IHJldHVybnMg bmlsICh3YyBpcyBmYWxzZSBpbiBydWJ5KQ0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg== |
|
#8
| |||
| |||
| 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. |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| RegExp help | usenet | PHP | 5 | 11-11-2007 09:02 PM |
| Why, oh, why, little regexp? | usenet | RUBY | 14 | 10-31-2007 01:25 PM |
| Help on RegExp | usenet | Javascript | 0 | 04-24-2007 05:56 PM |
| help with regexp | usenet | awk | 2 | 04-08-2007 04:48 AM |
| regexp | usenet | Perl | 3 | 12-17-2006 10:13 PM |
All times are GMT -5. The time now is 12:40 AM.



/
