Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JToken.SelectToken with regex for finding values that match a pattern?

How can I use JToken.SelectToken for finding values inside a path that match a pattern (e.g. find all values that are valid email addresses)? Is there any Regex compatibility like other frameworks support?

like image 461
Amir Popovich Avatar asked Nov 29 '25 16:11

Amir Popovich


1 Answers

I'm not sure if this is documented anywhere (I didn't find at least), but actually in last versions (starting with 11.0.1 it seems) it does. Syntax is =~ /regex here/. For example:

JObject o = JObject.Parse("{\"Objects\": [{\"Email\": \"[email protected]\"}, {\"Email\":\"not an email\"}]}");
// returns only "[email protected]" token
var result = o.SelectToken(@"$.Objects[?(@.Email =~ /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)]");
like image 54
Evk Avatar answered Dec 02 '25 07:12

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!