dimanche 20 janvier 2019

Lua - Get first occurrance of substring using a pattern

I have a string that looks like:

local str = "rootFolder\\<subFolder>\\<...>\\nFolder\\fileName";

where <...> could be a list of other folder names making the path/string very long. Also, I do not know what <subFolder> will actually be called since the folder name could be anything, i.e:

  • rootFolder\\folderA\\...
  • rootFolder\\folderB\\...
  • rootFolder\\folderC\\...
  • ...

We can assume I know the name of the root folder because this will be known at runtime, so for now lets assume it is called rootFolder.

How can I extract the sub-string <subFolder> using a pattern to match against str?

I was thinking of something like:

string.match(str, "rootFolder\\(.*)\\.*"); to capture the first folderName under rootFolder in the folder/directory hierarchy and ignore anything else that follows it but this is not working because, although it does match, it also gets everything else that follows it and not just the part I need (I also tried using .+ instead of .*).

Hope that makes sense. Thank you.

Aucun commentaire:

Enregistrer un commentaire