The input string for regex is a casual number of chars from another string. So it can be an incomplete part of a word at the end of the input string. Regex must cut the incomplete part of the word and the space at the end of the string. Because there must be the whole word at the end of this string. So I wrote this to cut the incomplete part of the last word (C#):
Regex.Replace(s, @"([^\s]*){1}$", String.Empty)
As you see I use the space as the mark for the regex to stop cutting. But how to cut this space also? It must be in the same regex because I don’t want to use another regex or “Trim” functions for this simple operation. I know that it is easier to use TrimEnd but it is not graceful. So it’s just academic question. 🙂
You can just include an optional space like so:
\s?([^\s]*){1}$Which can be simplified:
\s?\S*$