in reply to searching backwards in a string

anchor the search using the $ meta caracter (which represents end of string)

fruitless extraction of trailing whitespace:

/(\s+)$/ $num_trailing_spaces=length $1;
highly recommend oreilly book: Master REgular Expressions