# Regex and lookahead to specific number of chars

**URL:** <https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763>\
**Category:** Ruby\
**Created:** [February 25, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763 "2008-02-25T21:28:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 25, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/1 "2008-02-25T21:28:44Z")

</div>

If I have a string such as  
“I love ruby, i really do love ruby, oh yes i do blah blah blah”

and i want to insert an asterix at every 10th char whether its a  
whitespace, digit whatever how do i go about it

i was trying

string.gsub!(/[.]{10}/, “\*”)

but it doesnt work

any hints???

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 25, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/2 "2008-02-25T21:40:52Z")

</div>

Le 25 février à 21:28, Adam A. a écrit :

> and i want to insert an asterix at every 10th char whether its a  
> whitespace, digit whatever how do i go about it
> 
> i was trying
> 
> string.gsub!(/[.]{10}/, “\*”)

What about string.gsub!(/(.{9})./, ‘\1\*’) ?

Fred

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 25, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/3 "2008-02-25T22:40:36Z")

</div>

On 25.02.2008 21:28, Adam A. wrote:

> but it doesnt work
> 
> any hints???

Remove the square brackets around the dot. The dot is meta only outside  
of them.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 27, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/4 "2008-02-27T23:40:44Z")

</div>

Thanks Fred and Robert for that.

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?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 27, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/5 "2008-02-27T23:42:59Z")

</div>

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???

i wish there some regex exercises out there instead of the usual  
tutorial only stuff.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 27, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/6 "2008-02-27T23:51:59Z")

</div>

Adam A. 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.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [February 28, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/regex-and-lookahead-to-specific-number-of-chars/186763/7 "2008-02-28T12:51:00Z")

</div>

Le 27 février à 23:40, Adam A. a écrit :

> Thanks Fred and Robert for that.
> 
> 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?

It’s a reference to the first parenthesied group. To decompose the  
expression :

( =\> begin group 1  
.{9} =\> find exactly 9 characters  
) =\> end group 1  
. =\> followed by exactly one character

I replace that whole group by the first group (my 9 characters) and a  
star.

There are best ways to do it (especially if you’re manipulating very big  
strings, I think), but I think it’s the clearest.

Fred
