Question

My text file has more than ten thousand lines. Each line starts with a word or a phrase followed by a tab and the content, such as:

[line 1] This is the first line. [tab] Here is the content.[end of line]

I want to find character s in all the words between the beginning of each line and a tab (\t), and replace it by a pipe (|) so that the text will look like:

[line 1] Thi| i| the fir|t line. [tab] Here is the content.[end of line]

Here is what I have done:

Search: ^(.*)s+(.*)?\t 
Replace: \1|\2\t

It works but the problem is it does not replace s in one replace. I have to click on Replace All for several times before s in all the words is replaced.

So it comes to my question: how can I replace all the occurrences of character s in just one search and replace?

Note that I'm working on TextWrangler but I'm OK with other editors.

Thanks a lot.

Was it helpful?

Solution

You are searching for lines containing an s and do the match. Instead you should be searching for the s directly, and use lookahead to ensure that it is followed by a tab.

Search: s(?=.*\t)
Replace: |

Note that this catches all s's up to the last tab. - This will be a problem if your main content can contain tabs.

To stop catching s's after the first tab you have to cheat. Since variable length negative lookbehind doesn't work in AFAIK any regexp dialect.

However if we can ensure that the last s catches the whole line...

Search: (?:(^[^s\t]*\t.*$)|s([^s\t]*(?:(?=s.*\t)|\t.*$)))
Replace: |\1\2

This will catch the whole line in the case where no s occurs before the first tab. And put a | in front of that line. I see no way around this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top