Drake
Drake

Reputation: 2461

Grabbing a string using regex

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.

Upvotes: 0

Views: 533

Answers (3)

nall
nall

Reputation: 16149

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

Upvotes: 3

Scott Evernden
Scott Evernden

Reputation: 39986

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

Upvotes: 2

Joe
Joe

Reputation: 2587

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\(([^,]*),

Upvotes: 5

Related Questions