Page 1 of 1

Why doesn't this work?

Posted: Mon Sep 30, 2013 1:05 pm
by pb4072
Hi,
I need to keep the first word of every line, but, remove anything thereafter. I need to do this for each line of a 150 line file.

I'm searching for this:

^(.*$)(.*)$

I'm replacing with this:

\1

When I run this, it's removing everything. I'm left with blankness. Interestingly, in version 5 of Textpad, which I had until a few minutes ago, it simply replaced every line with itself. It didn't remove anything. But, now, with version 7, it's removing everything. Whatever. I just want it to keep the first parenthetical expression. Why doesn't it?

Thanks,
Peter

Posted: Mon Sep 30, 2013 1:48 pm
by ben_josephs
$ matches the end of a line (not the end of a word). So your first parenthesised subexpression matches the whole line. The second one matches everything that's left on the line: that is, nothing.

Try
Find what: ^(\S+).* [That's an upper-case S]
Replace with: $1
which keeps everything up to (but not including) the first white space character on each line.

I've used the new-style $1 instead of \1 in the replacement expression to represent what was matched by the first parenthesised subexpression (although it happens that \1 still works).