Yasston
Yasston

Reputation: 35

Trouble with regex capturing group & lazy match

Using .Net C#, I want to match the names in the $select clause in the follownig :

http://serveraddress/ODataServ/WcfDataService.svc/Persons?$select=LAST_NAME_IDNT,FIRST_NAME_IDNT&$top=1&$filter=Id eq '0000'

So I would like to have "LAST_NAME_IDNT,FIRST_NAME_IDNT" (i'll split it later based on the comma)

The $select clause can be in the middle of the URL, or at the end, i.e :

http://serveraddress/ODataServ/WcfDataService.svc/Persons?$top=1&$filter=Id eq '0000'&$select=LAST_NAME_IDNT,FIRST_NAME_IDNT

So I wrote the following pattern : (\$select\=)(.[^&])*(&|$) and tried to get Group 2. I don't know if it's because of lazy matching, but group 2 only contains like two characters : like "NT" in my example.

I don't know how to make it so that I can retrieve "LAST_NAME_IDNT,FIRST_NAME_IDNT"

Thanks in advance

Upvotes: 1

Views: 41

Answers (2)

Vaibhav Gupta
Vaibhav Gupta

Reputation: 401

The regex that you are using :

(\$select\=)(.[^&])*?(&|$)

(.[^&]) - this capturing group captures two characters together. any character and not &.

Now (.[^&])*? means that 0 or more set of these two characters with lazy match. Thus the capturing group will always have the last two characters of the match

Use this instead:

(\$select\=)([^&]*)

Upvotes: 1

karthik manchala
karthik manchala

Reputation: 13640

Your grouping is wrong.. use the following:

(\$select\=)([^&]*?)(&|$)        //also no need for '.'
            ↑      ↑

See DEMO

Upvotes: 1

Related Questions