How to find and replace in a text editor?

时间:2015-07-13 21:11:26

标签: regex replace textwrangler

I am very new to text editing, so I'm sorry if this question is unclear, let me know if there's anything I can specify to make my question more understandable.

My file has 27 tab-separated columns and thousands of rows. I want to replace tabs with an underscore (basically merging the first 3 columns together), but only after my first two columns. How do I do this?

Here's what I currently have for my find:

([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([ ^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^ \t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\r

and then here's my replace:

\1_\2_\3\t\4\t\5\t\6\t\7\t\8\t\9\t\10\t\11\t\12\t\13\t\14\t\15\t\16\t\17\t\18\t\19\t\20\t\21\t\22\t\23\t\24\t\25\t\26\t\27\r

Also, any references to a good regex guide would be welcomed!

Below are representative data. Each number is separated by a tab in my editor, not by a space.

chr1 28404 29751 25 14 57 42 44 44 56 34 16 24 18 24 24 23 24 163 57 30 28 31 36 23 28 17

chr1 235561 236222 5 13 4 24 4 8 7 6 5 14 20 7 10 3 6 11 9 9 16 8 16 6 11 9

chr1 540455 541272 20 11 6 7 5 7 12 24 7 9 9 6 22 3 10 32 18 22 11 13 10 27 9 10

chr1 713112 715467 96 105 332 159 131 277 225 199 61 164 128 116 156 107 143 687 204 186 97 125 174 193 213 118

chr1 761657 764380 106 153 334 182 161 326 215 343 85 174 160 135 176 151 141 724 308 223 120 141 200 198 247 151

2 个答案:

答案 0 :(得分:0)

试试这个 查找:

(.+?)\t(.+?)\t(.+?)\n

替换为

\1_\2_\3\n

查看Demo

此外,您必须禁用"。匹配New Line"在你的文本编辑器中。

答案 1 :(得分:0)

所以,你有类似

的东西

enter image description here

运行此搜索并在下面替换,您将获得:

enter image description here

正则表达式:

^(\s*(?:[^\t]+\t){2})([^\t]+)\t([^\t]+)\t

替换:$1$2_$3_

如果您可以有空列,请将+量词替换为*

^(\s*(?:[^\t]*\t){2})([^\t]*)\t([^\t]*)\t