How to Delete Everything in a Variable After a Given Set of Characters

Hi everyone!

I'm trying to do something with a macro, and I feel RegEx is the way to go, but I'm having a hard time understanding the logic behind it.

I have variables containing spaces, hyphens and/or underscores and I would like to delete everything after the first iteration of one of these characters.

For example :
35_Project_Subproject >> 35 (deleting everything after the first underscore)
35 Project Subproject >> 35 (deleting everything after the first space)
35-Project-Subproject >> 35 (deleting everything after the first hyphen)

Could you please help me figure out the correct way to do it and the logic behind it?

Thanks so much in advance,

Are the first characters of each line, prior to the “special” character, always digits?

this should work using the KM Search and Replace action using regular expressions:

Search For:
(?m)^(.+?)[_ -].+

Replace with:

For details, see: regex101: build, test, and debug regex

1 Like

Hi tiffle,
No, it can be any character sequence between all digits, all letters, or a combination of the two, the idea would be to keep the first word, whether it's made of letters or numbers :slight_smile:

Hi JMichaelTX,
Thanks a lot for the link that explained your RegEx, it allows me to understand the language a little bit better :slight_smile:
I'll implement that in the macro and get back to you, thanks a lot :wink::+1:

Hi again!
It works, thanks again a lot for your help, this little expression does a lot of magic for only 17 characters :wink:

1 Like

@JMichaelTX‘s suggestion will solve it for you!

1 Like

You're welcome. I use for all of my RegEx development and test. It's a great tool.