Search code examples
regexpcre

regex to match until a colon is found with or without quotes present


I'm looking for a regex to achieve the following:

When my input is root:Folder1:fname I want to get fname

and when the input string is root:Folder1:'fname' to get again fname (no quotes!).

For the former [^:?]*$ seems to get the job done but I cannot find a single regex to match both cases.

Is it possible to do it with a single regex? Tried to achieve it with conditional regex but I couldn't make it.

Any other tip/solution is welcome.

Thank you.

edit: The format of the paths is not fixed, quotes might appear elsewhere (e.g. root:'Folder with spaces1':fname or root:'Folder with spaces1':'fname'). The solution I guess is to search backwards as the filename we want to capture is always after the last colon.


Solution

  • If you want to enforce paired quotes, you need to capture the first quote.

    (\w+:)*('?)(\w+)\2$
    

    The expression inside the third pair of parentheses will be available as the third captured group ($3 or \3 or match.group(3) or what have you).

    If your regex dialect permits non-grouping parentheses, you can rearticulate this as

    (?:\w+:)*('?)(\w+)\1$
    

    and/or also perhaps extend the quote group to permit either single or double quote (['"]?pro '?). Then of course the final group will be $2 instead of $3.