#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    11

    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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"
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo