开发者

Remove a newline before a regex-matched line

I have 5 lines

line1
line2
line3
line4
line5
开发者_C百科

Using regex I've matched the line3. Now is there I way I can move the line3 just after line2(in other words, do away with the \n at the end of line2)?

I plan on using line3.sub(/myregex/, "some way to pull up line3 right after line2") method.

or I can do line2.sub(/regex_to_select_the_/n_at_the_end/, ""). Will this work?

Is there a better/different way to do it?


Just put the regex you used to match line3 into a lookahead expression, search for a \n before that and replace it with nothing:

result = subject.gsub(/\n(?=regex)/, '')

(where regex is your regex).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜