From: Paul Lutus Date: 2006-11-25T04:48:02+09:00 Subject: Re: Ruby with regular expression 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. 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? Why not open and read the file to find out whether it is simple or not? That will give you more confidence to forge ahead with the more interesting parts of your project. > but the expression to identify and then replace those numbers > are giving me problems. What problems? Please post your code and explain what went wrong. > Any help would be greatly appreciated. Not tested, but something like: data.sub!(%r{\[(.*?)\]},"12345") -- Paul Lutus http://www.arachnoid.com