Ruby with regular expression

Discussion in 'Ruby' started by peppermonkey, Nov 22, 2006.

  1. peppermonkey

    peppermonkey Guest

    Hi,
    I'm new to Ruby (and Regular expressions in general) and as such,
    this is rather a newbie question but I'm trying to write a script in
    Ruby to open a file and do some string manipulation.
    Basically I'm getting hung up with regular expressions.
    Suppose there is a file that contains, among other lines, lines of the
    following structure.

    "This [1234567890] points to [0987654321]"

    Assume the line structure (words, spaces etc.) stay constant.
    The actual numbers (within the brackets) are not constant.
    There is always 10 numbers within the brackets.
    There are other lines that have numbers enclosed by '[',']' that must
    be left alone.

    How can you replace only those numbers in the above structure to
    "xxxxxxxxxx"?
    Openning a file, searching the file, writing to the file I'm presuming
    is simple but the expression to identify and then replace those numbers
    are giving me problems. Any help would be greatly appreciated.

    Thank you.
     
    peppermonkey, Nov 22, 2006
    #1
    1. Advertising

  2. peppermonkey

    peppermonkey Guest


    > What distinguishes the lines you _do_ want to change from the ones you
    > _don't_ want to change? You don't say. Do all of them have ten digits?
    >
    > > How can you replace only those numbers in the above structure to
    > > "xxxxxxxxxx"?
    > > Openning a file, searching the file, writing to the file I'm presuming
    > > is simple

    > You're presuming?


    Sorry, should have made it clearer.
    The lines that I do want changed has the exact structure of

    "This [] points to []"

    with exactly 10 digits inside the brackets.
    There are no other lines with that structure.
    There are however, other lines that have numbers enclosed by the square
    brackets and they are to remain untouched.

    Not at home right now so will have to wait till then to work on it.

    Thanks
     
    peppermonkey, Nov 22, 2006
    #2
    1. Advertising

  3. peppermonkey wrote:
    > Hi,
    > I'm new to Ruby (and Regular expressions in general) and as such,
    > this is rather a newbie question but I'm trying to write a script in
    > Ruby to open a file and do some string manipulation.
    > Basically I'm getting hung up with regular expressions.
    > Suppose there is a file that contains, among other lines, lines of the
    > following structure.
    >
    > "This [1234567890] points to [0987654321]"
    >
    > Assume the line structure (words, spaces etc.) stay constant.
    > The actual numbers (within the brackets) are not constant.
    > There is always 10 numbers within the brackets.
    > There are other lines that have numbers enclosed by '[',']' that must
    > be left alone.
    >
    > How can you replace only those numbers in the above structure to
    > "xxxxxxxxxx"?
    > Openning a file, searching the file, writing to the file I'm presuming
    > is simple but the expression to identify and then replace those numbers
    > are giving me problems. Any help would be greatly appreciated.
    >
    > Thank you.


    This will modify the original file and create a backup file.
    (And it should be a single line.)

    ruby -i.bak -pe"sub(/This \[\d{10}\] points to \[\d{10}\]/, 'This
    [xxxxxxxxxx] points to [yyyyyyyyyy]')" myfile
     
    William James, Nov 22, 2006
    #3
  4. peppermonkey

    peppermonkey Guest

    William James wrote:
    > peppermonkey wrote:
    > > Hi,
    > > I'm new to Ruby (and Regular expressions in general) and as such,
    > > this is rather a newbie question but I'm trying to write a script in
    > > Ruby to open a file and do some string manipulation.
    > > Basically I'm getting hung up with regular expressions.
    > > Suppose there is a file that contains, among other lines, lines of the
    > > following structure.
    > >
    > > "This [1234567890] points to [0987654321]"
    > >
    > > Assume the line structure (words, spaces etc.) stay constant.
    > > The actual numbers (within the brackets) are not constant.
    > > There is always 10 numbers within the brackets.
    > > There are other lines that have numbers enclosed by '[',']' that must
    > > be left alone.
    > >
    > > How can you replace only those numbers in the above structure to
    > > "xxxxxxxxxx"?
    > > Openning a file, searching the file, writing to the file I'm presuming
    > > is simple but the expression to identify and then replace those numbers
    > > are giving me problems. Any help would be greatly appreciated.
    > >
    > > Thank you.

    >
    > This will modify the original file and create a backup file.
    > (And it should be a single line.)
    >
    > ruby -i.bak -pe"sub(/This \[\d{10}\] points to \[\d{10}\]/, 'This
    > [xxxxxxxxxx] points to [yyyyyyyyyy]')" myfile


    Thank you!
    Looking at the solution makes me feel dumb. Should have come up with
    that. Wasn't thinking simple enough. So yes, this is exactly what I was
    looking for. Thanks! Greatly appreciated!

    Hubert
     
    peppermonkey, Nov 22, 2006
    #4
  5. On Sat, Nov 25, 2006 at 04:48:01AM +0900, peppermonkey wrote:
    } Hi,
    } I'm new to Ruby (and Regular expressions in general) and as such,
    } this is rather a newbie question but I'm trying to write a script in
    } Ruby to open a file and do some string manipulation.
    } Basically I'm getting hung up with regular expressions.
    } Suppose there is a file that contains, among other lines, lines of the
    } following structure.
    }
    } "This [1234567890] points to [0987654321]"
    }
    } Assume the line structure (words, spaces etc.) stay constant.
    } The actual numbers (within the brackets) are not constant.
    } There is always 10 numbers within the brackets.
    } There are other lines that have numbers enclosed by '[',']' that must
    } be left alone.
    }
    } How can you replace only those numbers in the above structure to
    } "xxxxxxxxxx"?
    } Openning a file, searching the file, writing to the file I'm presuming
    } is simple but the expression to identify and then replace those numbers
    } are giving me problems. Any help would be greatly appreciated.

    It sounds to me like you are trying to substitute strings by reference,
    similar to localization. Based on that, I am assuming that you have a hash
    somewhere that maps these ten-digit numbers to the strings that should
    replace them. In that case, what you want is something like this:

    str.gsub(/\[(\d+)\]/) { hash[$1.to_i] }

    } Thank you.
    --Greg
     
    Gregory Seidman, Nov 25, 2006
    #5
    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. VSK
    Replies:
    2
    Views:
    2,397
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    886
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
  4. Wolfgang Nádasi-Donner
    Replies:
    10
    Views:
    310
    Wolfgang Nádasi-Donner
    Jun 30, 2006
  5. Ruchira Bomiriya

    Newby Ruby Regular Expression Question

    Ruchira Bomiriya, Jun 12, 2008, in forum: Ruby
    Replies:
    4
    Views:
    138
    Ruchira Bomiriya
    Jun 12, 2008
Loading...

Share This Page