Reputation: 203
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?
Upvotes: 5
Views: 7275
Reputation: 1
In Word text, if I want to highlight the last a in para. I search for all the words that have [space][para][space] to make sure I only have the word I want, then when it is found it should be highlighted. Next, I search for the last [a ] space added, in the selection and I will get only the last [a] and I will highlight it or color it differently.
Upvotes: -1
Reputation: 626845
"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.
Upvotes: 7