Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 24th, 2008, 03:24 PM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 68 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 27 m 23 sec
Reputation Power: 5
Send a message via MSN to karym6
Searching for text in files

Hi All,

I am having trouble putting some Ruby together to search through some lines of text in a specific file.

I have one file, called siteowner.aspx, that contains a certain string that would like to compare against a static value. So far, I can pick the correct line out, but I would like to filter it for a certain string.

So far, my code is as follows:

Code:
File.open("\\path\\to\\site.aspx", 'r') do |infile|
while (line2 = infile.gets)
     
       puts 'Via Regxp, Site owner is: '+ line2 if line2 =~ (/^= /) 
    end
end


However, I am stuck with the regular expression. The above code gives me:

'Via Regxp, Site owner is: siteowner = "OWNER"

But what I would really like is:

'Via Regxp, Site owner is: "Owner".

I dont suppose someone could help me out a bit and show me where I have gone wrong?

Reply With Quote
  #2  
Old April 24th, 2008, 04:45 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 51 m 38 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
Code:
irb(main):004:0> s = "siteowner=OWNER"
=> "siteowner=OWNER"
irb(main):005:0> puts "Owner is: #{$1}" if s =~ /.+=(\S+)/ 
Owner is: OWNER

You can use grouping to get a specific portion of a regular expression.

additionally
Code:
irb(main):008:0> "OWNER".capitalize
=> "Owner"
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Searching for text in files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway