Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a string variable like this;

D:\TZC\MTConnectAgent\

I'd like to get everything after the 2nd occurrence of \

So the result should be MTConnectAgent\

Any help would be greatly appreciated.

Thanks,

What I have tried:

I have tried this;

(?:.*?\\){2}

But it returns the first part of the string not the last part.
Posted
Comments
Maciej Los 7-Feb-24 16:41pm    
Do you mean something like this: regex101: build, test, and debug regex[^]?

You build and test your own expressions at https://regex101.com.
 
Share this answer
 
Take a look at this pattern: ^((?>\w\:\\.*?\\))(.*?)\\

More at: regex101: build, test, and debug regex[^]
 
Share this answer
 
Try this pattern (?:[^\\]*\\){2}(.*)
You can validate the test cases using RegExr: Learn, Build, & Test RegEx[^]
 
Share this answer
 
This is a .net example:

C#
(?n)(?<=^([^\\]*\\){2}).*


RegexTester[^]
 
Share this answer
 
v3

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