# Reg Ex

**URL:** <https://www.ruby-forum.com/t/reg-ex/114003>\
**Category:** Ruby\
**Created:** [September 7, 2007, 12:58am UTC](https://www.ruby-forum.com/t/reg-ex/114003 "2007-09-07T00:58:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 12:58am UTC](https://www.ruby-forum.com/t/reg-ex/114003/1 "2007-09-07T00:58:28Z")

</div>

I’m querying through the .com’s looking for any www name with Cisco in  
it.  
I’m using a reg exp that is reading a file line by line obtained by  
Verisign that has all the domain names with a .com extension.  
Here is my reg exp:

file.each { |line| print line if line =~ /(C|c)isco|CISCO/ }

but I’m getting results like SanFrancisco and Francisco.

Does anyone know how I can modify my reg exp to not include certain  
keywords like SanFrancisco and Francisco?

-Chuck

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 1:14am UTC](https://www.ruby-forum.com/t/reg-ex/114003/2 "2007-09-07T01:14:34Z")

</div>

On Sep 7, 2007, at 12:58 AM, Charles P. wrote:

> Does anyone know how I can modify my reg exp to not include certain  
> keywords like SanFrancisco and Francisco?

You need to assert word boundaries:

/\b((C|c)isco|CISCO)\b/

– fxn

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 1:11am UTC](https://www.ruby-forum.com/t/reg-ex/114003/3 "2007-09-07T01:11:33Z")

</div>

Hi

I don’t know if you need to check the caps, but your regex could be  
/cisco/i  
(the i is for case insensitive)  
To “ban” certain words, you’ could use lookaround expression -and I  
don’t  
know if Ruby supports it-, but I personally find cleaner check  
programatically against a list, and add the “exceptions” to that list.

Hope it helps 🙂

Diego.

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 10:42am UTC](https://www.ruby-forum.com/t/reg-ex/114003/4 "2007-09-07T10:42:15Z")

</div>

The boundaries could be a good idea, but if he needs urls like  
[ciscosystems.com](http://ciscosystems.com), with the final \b won’t match. Keep that in sight 🙂

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 11:54am UTC](https://www.ruby-forum.com/t/reg-ex/114003/5 "2007-09-07T11:54:29Z")

</div>

2007/9/7, Diego S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > How about:  
> > /\b([Cc]isco|CISCO)\b/
> > 
> > or even
> > 
> > /\bcisco\b/i

I’d probably use a second rx like

file.each { |line| print line if line =~ /\bcisco/i && line !~  
/sanfrancisco/i}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 2:06am UTC](https://www.ruby-forum.com/t/reg-ex/114003/6 "2007-09-07T02:06:36Z")

</div>

On Fri, 7 Sep 2007, Charles P. wrote:

> file.each { |line| print line if line =~ /(C|c)isco|CISCO/ }  
> …  
> Does anyone know how I can modify my reg exp to not include  
> certain keywords like SanFrancisco and Francisco?

How about:  
/\b([Cc]isco|CISCO)\b/

or even

/\bcisco\b/i

?

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/reg-ex/114003/7 "2007-09-07T18:26:47Z")

</div>

> Posted by Charles P. (chuckdawit) on 07.09.2007 00:58  
> I’m querying through the .com’s looking for any www name with Cisco in it.  
> I’m using a reg exp that is reading a file line by line obtained by  
> Verisign that has all the domain names with a .com extension.  
> Here is my reg exp:
> 
> file.each { |line| print line if line =~ /(C|c)isco|CISCO/ }
> 
> but I’m getting results like SanFrancisco and Francisco.
> 
> Does anyone know how I can modify my reg exp to not include certain  
> keywords like SanFrancisco and Francisco?
> 
> -Chuck  
> Reply with quote

Maybe also try something like: print line if line =~  
/^(?!._(?:SanFrancisco|Francisco))._\Bcisco/i

Cheers

j.k.

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 7, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/reg-ex/114003/8 "2007-09-07T20:27:32Z")

</div>

On Sep 6, 4:58 pm, Charles P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does anyone know how I can modify my reg exp to not include certain  
> keywords like SanFrancisco and Francisco?

Instead of modifying the regex, how about simply working on the data  
until it’s right?

ACCEPTABLE = [/francisco/i, /scisco/i]  
matches = file.readlines.select{ |line| line =~ /[Cc]isco|CISCO/ }  
ACCEPTABLE.each{ |re| matches.delete\_if{ |line| line =~ re } }  
puts matches

---

<div class="post-metadata">

**Author:** ![animate123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@animate123](https://www.ruby-forum.com/u/animate123)\
**Post date:** [September 11, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/reg-ex/114003/9 "2007-09-11T23:42:27Z")

</div>

Gavin K. wrote:

> On Sep 6, 4:58 pm, Charles P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Does anyone know how I can modify my reg exp to not include certain  
> > keywords like SanFrancisco and Francisco?
> 
> Instead of modifying the regex, how about simply working on the data  
> until it’s right?
> 
> ACCEPTABLE = [/francisco/i, /scisco/i]  
> matches = file.readlines.select{ |line| line =~ /[Cc]isco|CISCO/ }  
> ACCEPTABLE.each{ |re| matches.delete\_if{ |line| line =~ re } }  
> puts matches

If I want to print the line to a new file I use this:  
dnsfile.each { |line| newfile.puts line if line =~  
/(\b((C|c)isco|CISCO)\b) /

what can I do if I want to print it to a newfile and print it to the  
screen at the same time?
