Reputation: 19937
Using Regex
POSIX. Given a sentence, how can I extract all but the last word + the last word (preferably without the space between "to" and "2")? In my case, the last word will always be a number.
E.g.:
The string: Let's count from 1 to 2
is split into Let's count from 1 to
and 2
Upvotes: 0
Views: 2108
Reputation: 3305
If your last word will always be a digit:
/^([\S ]+) (\d+)$/
Upvotes: 3
Reputation: 185015
Try doing this :
/(.*?)\s+(\w+)$/
Check http://regex101.com/r/zK3iQ2/1
Upvotes: 2