查找多个子模式,并使用textwrangler grep在多行上使用后备副本替换

时间:2014-01-23 14:55:59

标签: regex grep textwrangler

目前我正在使用带有grep find / replace的TextWrangler(mac),但是同样乐意使用任何其他编辑器或命令行工具。

我有一个文本文件,其结构如下(是的,每行的开头都有一个空格):

 Reference 1 -  This is a sentence with a period. And this exclaims! So does this one!
 Reference 2 -  This questions? And this, this one responds. But this YELLS!

我需要保留引用,但是将每个句子分成它自己的行,就像这样:

 Reference 1 -  This is a sentence with a period.
 Reference 1 -  And this exclaims!
 Reference 1 -  So does this one!
 Reference 2 -  This questions?
 Reference 2 -  And this, this one responds.
 Reference 2 -  But this YELLS!

我可以用它来保留引用和最后一个句子(复制/替换那里的换行符,这就是为什么最后的中断 - 否则它与其余的匹配该文件):

^([^-]+ -\s+)(?:([^.!?]+?[.!?]))(([^.!?]+?[.!?])+?)$    

替换是这样的:

\1\2
\1\3

结果如下:

 Reference 1 -  This is a sentence.
 Reference 1 -   And this exclaims! So does this one!

 Reference 2 -  This questions?
 Reference 2 -   And this, this one responds. But this YELLS!

如果我多次运行它,它就不会将其他两个句子分成新行。 但是如果我在替换中添加另一行:

\1\4

然后我得到了这个结果:

 Reference 1 -  This is a sentence.
 Reference 1 -   And this exclaims! So does this one!
 Reference 1 -   So does this one!

 Reference 2 -  This questions?
 Reference 2 -   And this, this one responds. But this YELLS!
 Reference 2 -   But this YELLS!

我希望这很简单,我只是缺少一个开关/修饰符/等。

如果我一次只做一个句子,我不介意做其他的清洁工作。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

怎么样:

Search:
  ^( [^-]+-\s+)(.*[.!?]) *(.*[.!?])

Replace:
  \1\2
  \1\3

我必须经历几次,但它似乎与你的目标模式相匹配。