Link to home
Start Free TrialLog in
Avatar of jay_waugh
jay_waughFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Identify all characters following the final space in a string

I am trying to gather all of the characters following the final space in a string but am struggling with the syntax. Can anyone help please? The last set of characters will always be alphanumeric but will be of differing characters and amount of charaters.

In the below example I would only want to highlight cat.  

dog xxx-klsfkd-sdf-sdf cat


And in the next one aaaaaaaaaaaaaddddddddddddddd

heht ooijl-nanhjhsh aaaaaaaaaaaaaddddddddddddddd


Many Thanks
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Jay

Are you using Python for this question?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you wanted alphanumeric you can use this
[\w]+$

Open in new window

https://regex101.com/r/qlAarF/4
@Shaun

\w includes the underscore character.
What underscore do? No underscore in question
https://regex101.com/r/qlAarF/4
Which is why \w is probably not the correct pattern to use for this problem.
Works for all examples give by OP
@Shaun

The question text included this:
The last set of characters will always be alphanumeric

In your \w comment, you wrote this:
If you wanted alphanumeric you can use this

Since the \w pattern matches the underscore character, it would not be the correct pattern to suggest for this problem/question.  I was correcting your suggestion for future readers of this thread and educating the OP.  It doesn't matter whether it matches the sample text.
If the underscore bugs you so much, use
[^\W_]+$

Open in new window

https://regex101.com/r/YNqaHP/1
You're getting closer.  Try adding the space character at the start of your pattern.
Not required. If your interest was the future usability of solutions (which is clearly mine, posting after the question was closed) you would post solutions instead of trolling mine.

I'll post an image because clearly, you are not going to the links I post
User generated imageUser generated imageUser generated image