Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am stumped with a regular expression needed to find the correct string if the word FOUND is at the end of it I want to return anything before the match.

here is an example of the strings I am trying to sort through.

C:\Users\Admin1\Desktop\eicar.com: Eicar-Test-Signature FOUND
C:\Users\Admin1\Desktop\new process run code.txt: OK
C:\Users\Admin1\Desktop\Super-Charger.lnk: OK
C:\Users\Admin1\Desktop\Thumbs.db: OK


In the first string I want to get just the file location and filename like so.
C:\Users\Admin1\Desktop\eicar.com

I was hoping that there was someway to show everything before a match so that I can specify the regular expression to find the word "FOUND" and exclude the name to only show the file path.

This is the closest I can get to what I am looking for
(?<path>.+)(?<=FOUND)
this returns the correct file path based on the word FOUND but I need to cut the string Eicar-Test-Signature FOUND

Thank you in advance for any help you can provide
cheers!
Posted
Updated 26-Aug-13 15:50pm
v2
Comments
ArunRajendra 26-Aug-13 23:18pm    
I think that's the only option. You cant do both search and extract using regular expression.

1 solution

Pick up a copy of Expresso, here[^]. You would probably need to specify that you're look for everything from the beginning of the line so it picks up the text of a single line and not EVERYTHING form the beginning of the text to the first occurance of "FOUND".
 
Share this answer
 
Comments
Draco2013 27-Aug-13 1:58am    
thank you I have espresso already but when all occurrences start with C:\... ect its my only choice to find the string with the word found at the end how is it possible to trim it so only the file path is returned?

any example or close to would be a great help
Dave Kreskowiak 27-Aug-13 8:01am    
A capture group, starting at the beginning of the line (^) and ending where the "FOUND" is?
Draco2013 28-Aug-13 2:17am    
yes im sorry but I have tried to read and understand how this works but cannot seem to make sense of it. anything I try returns nothing?

How do I use the "^" to match the first part of a line and then use "$" to specify the end of a string?.. also once that is done how do I go about only showing the beginning of the line or match?

thank you in advance..
Draco2013 28-Aug-13 2:41am    
if your still able to help me understand this better I would appreciate but I have the desired result using string split

cheers!
Dave Kreskowiak 28-Aug-13 9:16am    
RegEx can be a bit tough to learn and figure out. The following expression should work:

(.+)(?=:\sEicar-Test-Signature\sFOUND$)

It'll return a numbered capture group with the filepath in it.

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