Link to home
Start Free TrialLog in
Avatar of K B
K BFlag for United States of America

asked on

Regex to find the last underscore and all text that follows it

My sample text would be something like this list

alpha123_4rf_Joe
45beta_Frank
Red5Great_Sam_Fun

and I would like to be left with (with a notepad++ regex find and replace)

alpha123_4rf
45beta
Red5Great_Sam

I am just looking for the Regex as I understand notepad++ :-)

Thank you!
Avatar of mccarl
mccarl
Flag of Australia image

The regex in the comment that you posted (and deleted) should work, although you should also be able to simplify that to...

_+[^_]*$

Open in new window


I don't know Notepad++ so I am not sure exactly how it would apply a regex but you may need to specify options to make it a "multiline" match and to match "globally" or all occurrences. Also, it may be obvious, but the replacement string should be just empty.
Avatar of Bill Prew
Bill Prew

I confirmed that _+[^_]*$ works in Notepad++ Replace as a REGEX, looks like a winner to me.

~bp
Avatar of K B

ASKER

Thank you for your replies.. it seems to not stop at the end of the line and highlight four additional lines (randomly?)
User generated image
And here is a test of the above regex...   https://regex101.com/r/fJ6hO6/1
Avatar of K B

ASKER

Here is a better example... perhaps I should have given this to begin with..

MEX05\c.frank_plaCAAD6
MEX05\e.steve_playa6CE8F
MEX05\d.harry
MEX05\kdog
MEX05\scat
MEX05\j.rabbit
MEX05\c.mark_play79B61
MEX05\l.fancy
MEX05\n.pants
MEX05\a.farley
MEX05\davidspade

Open in new window

It's very hard with the blurred image to know what is going on, but I understand that there might be data that you can't share with us!

Does the regex work with the original sample text? Can you come up with some sample text that DOES exhibit the issue that you have but that is non-sensitive data so that you can fully post it here?
What's different compared to test data you posted?

~bp
Avatar of K B

ASKER

Our comments just posted at the same time :-)
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

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
Avatar of K B

ASKER

awesome!
thank you!
You're welcome!
Try this:

_+[^_\n]*$

Open in new window

~bp