Click here to Skip to main content
15,885,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below string :
"ISOC Charges: Member 716 - LGIM_GASPEN_MG"

My requirement is to search set of characters in above string from right to left until we find first space. So, I should get the value = "LGIM_GASPEN_MG"

What I have tried:

I tried using regular expression but not getting how to traverse the string from right to left.
Posted
Updated 28-Dec-21 21:02pm
Comments
Peter_in_2780 29-Dec-21 2:00am    
Regex is the wrong tool for that job. Many languages include a "LastIndexOf()" or similar function. If your language of choice doesn't it's dead easy to roll your own. There is a simple regex to do what you want, but as I said, it's the wrong way to attack this problem.

You don't: regexes don;t work that way.
You can however trigger on the last space:
(?:\s)[^\s]+$

For an explanation of that, get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
 
Share this answer
 
Quote:
My requirement is to search set of characters in above string from right to left until we find first space. So, I should get the value = "LGIM_GASPEN_MG"

Since RE works left to right, you need to look at last space and skip anything before that last space.
This RE find last group of letters in string:
\w+$

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900