From: Mark Bush Date: 2008-02-28T07:51:51+09:00 Subject: Re: regex and lookahead to specific number of chars Adam Akhtar wrote: > I dont understand what the \1 does. I know if i remove it it removes all > the characters before the 10th and if i include it in keeps them. Why is > this? When you put a regexp in round brackets, what it matches is remembered. In a replacement string, you can then refer to them as \1, \2, etc. > oh and a totally newb question but when i try to substitute the asterix > with a newline \n it just prints it instead of creating a newline, why > is that??? Since the replacement string is in single quotes, \n is not special so represents the two characters \ and n. To get a newline, you need to put \n in double quotes ("\n"). In double quotes, the backslash quotes any character, so \1 would become the character with code 1. So in double quotes, the \1 needs to be \\1 so: string.gsub!(/(.{9})./, "\\1*") for the original example and: string.gsub!(/(.{9})./, "\\1\n") for the newline example. -- Posted via http://www.ruby-forum.com/.