0

I am having trouble figuring this one out. I need to be able to dereference an integer variable in ruby within a regex. So, for example, lets say I have a string called this_string, and I want to insert a space every 8 characters, this would work:

this_string.scan(/.{8}|.+/).join(" ")

But how do I do this every N characters using a regex, where N is an arbitrary integer?

1 Answer 1

2
n = 3
s.scan(Regexp.new ".{#{n}}|.+").join ' '
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.