regex last character of a WORD

I'm attempting to match the last character in a WORD.

A WORD is a sequence of non-whitespace characters '[^\n\r\t\f ]', or an empty line matching ^$.

The expression I made to do this is: "[^ \n\t\r\f]\(?:[ \$\n\t\r\f]\)"

The regex matches a non-whitespace character that follows a whitespace character or the end of the line.

But I don't know how to stop it from excluding the following whitespace character from the result and why it doesn't seem to capture a character preceding the end of the line.

Using the string "Hi World!", I would expect: the "i" and "!" to be captured.

Instead I get: "i ".

What steps can I take to solve this problem?


"Word" that is a sequence of non-whitespace characters scenario

Note that a non-capturing group (?:...) in [^ \n\t\r\f](?:[ \$\n\t\r\f]) still matches (consumes) the whitespace char (thus, it becomes a part of the match) and it does not match at the end of the string as the $ symbol is not a string end anchor inside a character class, it is parsed as a literal $ symbol.

You may use

\S(?!\S)

See the regex demo

The \S matches a non-whitespace char that is not followed with a non-whitespace char (due to the (?!\S) negative lookahead).

General "word" case

If a word consists of just letters, digits and underscores, that is, if it is matched with \w+, you may simply use

\w\b

Here, \w matches a "word" char, and the word boundary asserts there is no word char right after.

See another regex demo.