Search code examples
regexregex-lookarounds

What's the regex to match a string except it has a $ dollar sign in it?


So basically, I have a text like this:

secret(mapOf("path" to "config/info"))
secret(mapOf("path" to "config/data/${rootProject.name}"))
prefix(mapOf("path" to "config/${rootProject.name}", "format" to "${rootProject.name.replace('-', '_')}"))

I want to match a path like config/info and not to match paths which contain variables (have dollar signs in them). I came up with this ((?:'|\")config/.+(?:'|\")) but it also matches the others. How can I exclude strings with dollar signs?


Solution

  • What you are looking for (if I fully understand your question) is this one:

    (['\"]config/[^$]+['\"])
    

    Explanation:

    • I used ['"] instead of (?:'|\") I believe it will make your regex readable and simple to understand in the future.
    • Also using [^$]+ instead of .+ is the key to solve your issue, .+ will match anything however [^$]+ will match anything as long as it's not $.