# Regexp help

**URL:** <https://www.ruby-forum.com/t/regexp-help/114933>\
**Category:** Ruby\
**Created:** [September 18, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/regexp-help/114933 "2007-09-18T23:12:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 18, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/regexp-help/114933/1 "2007-09-18T23:12:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 18, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/regexp-help/114933/2 "2007-09-18T23:21:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 12:46am UTC](https://www.ruby-forum.com/t/regexp-help/114933/3 "2007-09-19T00:46:10Z")

</div>

On Sep 18, 4:04 pm, Jeremy W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.)

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 12:04am UTC](https://www.ruby-forum.com/t/regexp-help/114933/4 "2007-09-19T00:04:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 12:53am UTC](https://www.ruby-forum.com/t/regexp-help/114933/5 "2007-09-19T00:53:38Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 12:55am UTC](https://www.ruby-forum.com/t/regexp-help/114933/6 "2007-09-19T00:55:04Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 5:40am UTC](https://www.ruby-forum.com/t/regexp-help/114933/7 "2007-09-19T05:40:32Z")

</div>

From: Jeremy W. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [September 19, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/regexp-help/114933/8 "2007-09-19T13:38:44Z")

</div>

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.
