Use reqex to get the middle character and repeat it

say i have the string “qxq” and i want to repeat the middle char 4 times
i could do this
str = "qxq
newstring = str.gsub(/(x)/, ‘\1’*4)

but if i have a string full of x’s i.e. “xxx”
then it will repeat all of them 4 times and thats not what i want to
do…i just want the middle one to be repeated.

How do i alter that regex to do this???

Adam A. wrote:

say i have the string “qxq” and i want to repeat the middle char 4 times
i could do this
str = "qxq
newstring = str.gsub(/(x)/, ‘\1’*4)

but if i have a string full of x’s i.e. “xxx”
then it will repeat all of them 4 times and thats not what i want to
do…i just want the middle one to be repeated.

How do i alter that regex to do this???

str.gsub(/(\w)(x)(\w)/, ‘\1’+’\2’*4+’\3’)

HTH,
Sebastian

Don’t use a regexp. It isn’t clear what you want to happen if there
isn’t a middle character if the string has a odd integer for its length.
Try using some type of substring approach such as this:

str = “xxx”
str[0…str.length/2-1] +
str[str.length/2,1] * 4 +
str[str.length/2+1…str.length]
=> “xxxxxx”

Hi –

On Fri, 14 Mar 2008, Dan D. wrote:

Don’t use a regexp. It isn’t clear what you want to happen if there isn’t a middle character if the string has a odd integer for its length. Try using some type of substring approach such as this:

str = “xxx”
str[0…str.length/2-1] +
str[str.length/2,1] * 4 +
str[str.length/2+1…str.length]
=> “xxxxxx”

A slightly different way to do (I think) the same thing:

irb(main):036:0> str = “xxx”
=> “xxx”
irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
=> “xxxx”
irb(main):038:0> str
=> “xxxxxx”

I was hoping for a *= operation but it doesn’t quite fit :slight_smile:

David

ahh why didnt i just think of that… str.gsub(/(\w)(x)(\w)/,
‘\1’+’\2’*4+’\3’)

In this situation the string will always be 3 chars long and will either
consist of spaces or x so the above will be fine (with a bit of
tweaking).

Thanks very much!

string will always be 3 chars long

If the string is always 3 chars long you could simply do this

str[0…0] + str[1…1] *4 + str[2…2]

or this

str[0].chr + str[1].chr * 4 + str[2].chr

In this situation the string will always be 3 chars long and

this is not regex :slight_smile:

irb(main):007:0> RUBY_VERSION
=> “1.9.0”
irb(main):008:0> str=‘qxq’
=> “qxq”
irb(main):009:0> str[0]+str[1]*4+str[2]
=> “qxxxxq”

or more succinct,

irb(main):010:0> str[1]*=4
=> “xxxx”
irb(main):011:0> str
=> “qxxxxq”

kind regards -botp

On Fri, 14 Mar 2008, David A. Black wrote:

str[str.length/2,1] * 4 +
=> “xxxxxx”

I was hoping for a *= operation but it doesn’t quite fit :slight_smile:

Actually it does work in 1.9:

irb(main):004:0> str = “xxx”
=> “xxx”
irb(main):005:0> str[str.size/2] *= 4
=> “xxxx”
irb(main):006:0> str

:slight_smile:

David

David A. Black wrote:

irb(main):036:0> str = “xxx”
=> “xxx”
irb(main):037:0> str[str.size/2] = str[str.size/2].chr * 4
=> “xxxx”
irb(main):038:0> str
=> “xxxxxx”

I was hoping for a *= operation but it doesn’t quite fit :slight_smile:

str[str.size/2,1] *= 4

Dan D. schreef:

str[0].chr + str[1].chr * 4 + str[2].chr

or this… str[1,1]=str[1,1]*4