tags:

views:

84

answers:

3

I have the following string. I have broken it into two lines below for clarity but they are one line.

    WHEN NVL(somevar1, 0) > 0 THEN 
(CAST(NVL(somevar2, 0) AS
 FLOAT(53)) / CAST(NVL(somevar3, 0) AS FLOAT(53))) * 100

I want to write a Regex so that I can get somevar1, somevar2 and somevar3.

I was trying with something like this:

NVL(.*,)

But this matches the last comma instead of the first comma.

BTW, I am doing this in groovy.

+3  A: 

Your attempt is a greedy regex. Try /NVL\((.*?),/ and the results should be in backreference 1.

nall
+5  A: 

It'd help to know what regex engine you're using, as not all support non-greedy quantifiers.

Try one of the following:

NVL\((.*?),

or

NVL\(([^,]*),
Joe
I think it's better to use `NVL\((\w+?),`
SilentGhost
SilentGhost -- quite possibly, but I've even run into regex engines that didn't support it, as sad as that is. Also, I probably should've mentioned that mine's still greedy in a way -- it could include spaces on either side of the variable name (more like Drake's original), while using \w might require \s* on either side, or a post-match trim.
Joe
+2  A: 

I'd use \w ( or possibly [\w_$] ) instead of * so as to match 'word characters' (letters, digits)

Scott Evernden
you're mixing quantifiers and classes.
SilentGhost