On Tue, 15 Feb 2005 14:00:03 +0000, "MJ Ray" <mjr@dsl.pipex.com> said:
Richard Lewis wrote:
I need to match all the characters from either the beginning of the line or the last match (in global mode) up to the last space before the Xth character.
I can do the first space after the Xth character: $ echo "A string with quite a lot of words and spaces in it." | sed "s/\(.\{,X\}\) /\1\n/g"
Doesn't that do the last space before the (X+1)th character? I tested it a bit and it seems to. All you need to do is change the space to \( \|$\) so you don't always split the last word off, I think.
Um, on second thoughts, actually I don't think this does work (the difference is quite subtle). XSLT regex syntax allows: "* X{n}? matches X, exactly n times * X{n,}? matches X, at least n times * X{n,m}? matches X, at least n times, but not more than m times" of which I'm trying to use the second with the replace() function like this: replace($text, '(.{n,}?)\s+', '$1 ') [replace any character repeated at least n times (saved as $1) followed by any number and type of spaces with match $1 plus a line break] (I then split the string on new-line characters to display it using tokenize().) Um, yeah. This isn't really a question, but if anyone has any thoughts..... Richard PS. I'm just having an idea about taking exactly n characters then further processing the result by trying to find the last space......I'm not sure there /is/ a regex which does that. PPS. prove me wrong!