Reg Ex

C

Charles Pareto

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
 
X

Xavier Noria

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?

You need to assert word boundaries:

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

-- fxn
 
S

S P Arif Sahari Wibowo

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

?
 
R

Robert Klemme

2007/9/7 said:
The boundaries could be a good idea, but if he needs urls like
ciscosystems.com, with the final \b won't match. Keep that in sight :)

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

I'd probably use a second rx like

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

Kind regards

robert
 
J

Jimmy Kofler

Posted by Charles Pareto (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.
 
P

Phrogz

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?

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
 
C

Charles Pareto

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

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

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top