Regex not matching in C# -


i have following string:

 string error = "<messages><message severity=\"2\" number=\"16\" text=\"the record case locked user\" /></messages>"; 

i want match between text=\" , following \

i'm using following expression var regex = new regex(@"text=\\""(.*?)\\");

expresso tells me regex correct. regexr tells me regex correct.

but c# disagrees.

i've tried

  • groups[] , match.value.
  • \x22 instead of " thought might escape problem.
  • /text=\""(.*?)\/g

all, no avail.

what missing?

use xelement, have xml fragment:

var error = "<messages><message severity=\"2\" number=\"16\" text=\"the record case locked user\" /></messages>"; var xe = xelement.parse(error); var res = xe.elements("message")                    .where(p => p.hasattributes && p.attributes("text") != null)                    .select(n => n.attribute("text").value)                    .tolist(); 

output:

enter image description here

mind large input strings, .*? may cause catastrophic backtracking, why should avoid using whenever possible. if need regex (because of input not xml-valid), can use:

var attr_vals = regex.matches(error, @"(?i)\btext=""([^""]+)""")              .oftype<match>()              .select(p => p.groups[1].value)              .tolist(); 

(2 times faster karthik's, tested on regexhero.com)

output:

enter image description here

mind regex, xml entities untouched (e.g. &amp; , not &). have use system.web.httputility later.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -