【发布时间】:2021-12-19 03:02:20
【问题描述】:
我正在寻找一个 regexp,它可以从长字符串中找到特定的换行符 \n。
特定的\n 是在不包含特定字符的行之前:'#'
例如:
这很好#line1\n这是另一个很好#line2\nThis_belongs_to abobe line\n这还可以#line4
表示文本:
this tis a fine #line1
this tis another fine #line2
this_belongs_to abobe line
this tis still is OK #line4
这里\n要在#line2之后的那个中删除,导致在正文中:
this tis a fine #line1
this tis another fine #line2this_belongs_to abobe line
this tis still is OK #line4
我想出了一个正则表达式,例如:\n^(?m)(?!.*#).*$,它很接近,但我不知道如何构建正确的表达式,让我只匹配和删除正确的换行符并保留剩余的文本/字符串.
也许有比使用正则表达式更好的方法?
【问题讨论】:
标签: java regex regexp-replace