Bill McCarthy wrote:
Hello Vim List,

Example 1:

    :echo "<" . matchstr("  1.2345 ","[0-9.]") . ">"<CR>
    <1>

Example 2:
    :echo "<" . matchstr("  1.2345 ","[0-9.]*") . ">"<CR>
    <>

Why isn't the second exampe returning <1.2345>?

Is there a better way of stripping spaces off a string?


What you get is the first (leftmost) substring matching the pattern.

In the first case the pattern matches exactly one dot or digit, and it matches the 1 at position 2.

In the second case the pattern matches zero or more dots and digits, and it matches the null string at position 0.

If you had specified \+ as multi instead of * you would (IIUC) have got what you wanted.

See ":help /multi"


To "strip spaces off a string" I would have used substitute(string, " ", "", g)

What you are trying to do here is not "strip spaces off a string" but "extract the leftmost substring consisting only of one or more digits and periods, as many as possible". It might be different. Try it with " 2006.08.06 3:34 UTC ". ;-)

Best regards,
Tony.

Reply via email to