Stewart Stremler wrote:
begin  quoting Ralph Shumaker as of Thu, Feb 01, 2007 at 09:59:22PM -0800:
[snip]
How would I search for every numerical sequence *not* followed by a space, and append a space after each such instance?


In vi(m):

:%s/\([0-9][0-9]*\)\([^ 0-9]\)/\1 \2/g

:       -> command mode
%       -> for every line in the file
s       -> substitute
/       -> use / for delimiter
\(      -> start first group
[0-9]   -> any digit
[0-9]   -> any digit
* -> 0 or more of previous character (This gives the effect of [0-9]+)
\)      -> end first group
\(      -> start second group
[^ 0-9] -> every character except space and digits (and newlines)
\)      -> end second group
/       -> end "find" part, start "replace" part
\1      -> first group
\2      -> second group
/       -> end pattern
g       -> global, that is, replace all matches, not just the first on
           each line

The key is the [^ ] construct. [0-9] says "match any characters
zero through nine", while [^0-9] says "match any characters NOT
0-9 or newline" (since vi(m) is basically line-oriented).

I wish I had gone online and downloaded my email to see this before I found my own solution. I did man regex, and then did /\. and looked for any occurence of "." that was not end of sentence. I just had a feeling that it would yield what I wanted. I quickly found (and relearned) the utility of "^" as the NOT.

I was able to follow along with the formula you gave above (even without the explanation this time, although the bit about newlines was informative). I think I need to start using regex a lot more often so as to become proficient, learn more, and not forget what I already know (i.e. ^==NOT and that "0-9" can be combined with " ").

Although I would change (of your formula) the final g to c.

Thanks again.


--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to