我有5行
line1
line2
line3
line4
line5
使用正则表达式我匹配了line3。现在我可以在line2之后移动line3(换句话说,取消第2行末尾的\ n)?
我打算使用line3.sub(/myregex/, "some way to pull up line3 right after line2")
方法。
或者我可以line2.sub(/regex_to_select_the_/n_at_the_end/, "")
。这有用吗?
有更好/不同的方式吗?
答案 0 :(得分:3)
只需将用于匹配line3
的正则表达式放入先行表达式,在此之前搜索\n
并将其替换为空:
result = subject.gsub(/\n(?=regex)/, '')
(regex
是你的正则表达式。)