Reputation: 342
I'm trying to match strings similar to Lookup("lookup name", "lookup key")
so I can replace the "lookup key".
I've got a pattern where the lookup key is "3" or 3:
[lL][oO][oO][kK][uU][pP]\(.*?,[ ]*("3"|3)\)
But when I use it on the following input string (which has nested calls) it matches the entire string except for the last parenthesis.
LOOKUP("lookup name1",LOOKUP("lookup name2",3))
How do I get it to just match the last part LOOKUP("lookup name2",3)
?
Upvotes: 1
Views: 59
Reputation: 41838
Directly Matching and Replacing the Key
This matches your lookup key directly!
(?i)(?<=lookup\([^(),]*,)[^()]*?(?=\))
See demo.
You can then replace it with whatever you like:
resultString = Regex.Replace(yourString, @"(?i)(?<=lookup\([^(),]*,)[^()]*?(?=\))", "whatever");
This works because the .NET regex engine supports infinite lookbehinds.
Explanation
(?i)
puts us in case-insensitive mode(?<=lookup\([^(),]*,)
is a lookbehind that asserts that what precedes us is the literal lookup(
, then any characters that are not parentheses or commas, then a comma[^()]*?
lazily matches any characters that are not parentheses (this is our match!)(?=\)
asserts that what follows is a closing parenthesisReference
Upvotes: 4