Remove a newline before a regex

I have 5 lines

line1
line2
line3
line4
line5

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).

链接地址: http://www.djcxy.com/p/13466.html

上一篇: 按分隔符分割而不将其从字符串中移除

下一篇: 在正则表达式之前删除换行符