NuB: RegExp case-independent match

Discussion in 'Ruby' started by David Alm, Apr 28, 2006.

  1. David Alm

    David Alm Guest

    I'm new to both Ruby and RegExp.

    I'm wanting to do a string sub in lines of a file but have the match
    criteria for gsub be case-independent. The only think I could think of was
    to upcase the whole thing:

    content = File.readlines( x ).collect! { |line|
    if ( line =~ /someLineCriteria.*$/ )
    line.upcase!.gsub(/SOME_GSUB_CRITERIA/, $string_to_sub )
    else
    line
    end
    }

    But I would prefer not to have to upcase it all, and have the RE match
    "SomE_Gsub_CRITeria" or any similar combination of upper/lower case.

    Thanks in advance.

    -----
    Dave
    Herd Master
    Round Cow Systems
    www.roundcow.com
     
    David Alm, Apr 28, 2006
    #1
    1. Advertising

  2. Jeffrey Schwab wrote:
    > David Alm wrote:
    >> I'm new to both Ruby and RegExp.
    >>
    >> I'm wanting to do a string sub in lines of a file but have the match
    >> criteria for gsub be case-independent. The only think I could think of
    >> was to upcase the whole thing:
    >>
    >> content = File.readlines( x ).collect! { |line|
    >> if ( line =~ /someLineCriteria.*$/ )
    >> line.upcase!.gsub(/SOME_GSUB_CRITERIA/, $string_to_sub )
    >> else
    >> line
    >> end
    >> }
    >>
    >> But I would prefer not to have to upcase it all, and have the RE match
    >> "SomE_Gsub_CRITeria" or any similar combination of upper/lower case.
    >>
    >> Thanks in advance.

    >
    > After the last slash of the regex literal, add the modifier "i":
    >
    > /someLineCriteria.*$/i


    Also it's completely useless to use ".*$" here - it won't make a
    difference with regard to matches and only waste CPU cycles.

    Regards

    robert
     
    Robert Klemme, Apr 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike

    NuB datagrid question

    Mike, May 4, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    417
    Rick Spiewak
    May 4, 2004
  2. Agency

    Nub needs help withTkinter

    Agency, Dec 6, 2003, in forum: Python
    Replies:
    6
    Views:
    303
    Peter Otten
    Dec 9, 2003
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    543
    Mikel Lindsaar
    Mar 31, 2008
  4. Old Echo
    Replies:
    1
    Views:
    200
    Adam Shelly
    Sep 4, 2008
  5. Uldis  Bojars
    Replies:
    2
    Views:
    214
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page