Ross Bamford wrote:
> Hi,
> 
> On Wed, 2006-04-12 at 20:58 +0900, Peter Bailey wrote:
> 
>> 
>>     (irb):26: parse error, unexpected tSTRING_BEG, expecting ')'
>>     line.sub!(/\%\%Pages: ([0-9]{1,5})/"Pages: \1"/)
>>                                     ^
>>     (irb):26: parse error, unexpected ')'
>>            from (irb):29
>> 
>> Is there a significance to the ^ under the "P" in my syntax error? 
>> What's it telling me? My sub requires parentheses. Is there a conflict 
>> with my grouping parentheses inside my regular expression?
>> 
> 
> ruby is just complaining about a Perl-style s/// regexp. :) The correct
> syntax here would be something like:
> 
> 	line.sub!(/\%\%Pages: ([0-9]{1,5})/) { "Pages: #$1" }
> 
> (You can sometimes use the \1 escapes in the replacement but it's always
> come out hit-and-miss for me, so I stick to the block form).
> 
>> Another request for help--my translation above would basically move the 
>> entire contents of my source file to the target file, with the exception 
>> of the one line I want changed. That's overkill for me. I just want a 
>> few lines of information in my target file. Would you suggest I just do 
>> a simple search, using matching (=~), and then write about that find in 
>> my target file, or, would it be better to do mass deletions of 
>> everything I don't want from my source to my target files? In other 
>> words, should I say "in this file, find this, then say this . . ." or, 
>> "convert this entire file, but just change this?"
> 
> I would probably go with extracting just what I needed from the file,
> though if you can safely exclude portions of it before running matches
> (for example a large data section) that may improve performance.
> 
> Also, I don't know if there are any Ruby postscript libraries, but it
> might be worth a search if you haven't already...
> 
> Hope that helps,


Thank you, Ross.
Yes, my regex experience is indeed with Perl, and Perl only. I haven't 
seen anything else in my docs, though, about Ruby being different with 
regexes, except for the stuff about true objects with Regexp#match. Both 
your response and Peter's below, though, show me something entirely new. 
It looks like you're putting the replacement phrase into a block, 
correct? It's kind of ugly at first, I must say, but, I see some sense 
in it. Your suggestion worked for me, by the way. Now, I'd like to 
streamline my conversion and aim for the much smaller target file I 
mentioned above. Right now, my target file is still 10mB big, with just 
that one pages line replaced with what I want. So, instead of working in 
line mode, do you think I should just read in the entire file and use 
".*" before and after my sub expression to get rid of everything else?

-- 
Posted via http://www.ruby-forum.com/.