简体   繁体   中英

extract a value from a string using regex

I'm trying to extract a value from a string using regex. The string looks like this:

<faultcode>&lt;![CDATA[900015The new password is not long enough. PasswordMinimumLength is 6.]]&gt;</faultcode>

I am trying to diplay only the error message to end user.

由于您可能想要所有<![CDATA[]]>因此应该适合:

<!\[CDATA\[(.+?)\]\]>

The only sensible thing is to load it into an XElement (or XDocument, XmlDocument) and extract the Value from the CDATA element.

XElement e = XElement.Parse(xmlSnippet);
string rawMsg = (e.FirstNode as XCData).Value;
string msg = rawMsg.Substring("900015".Length);

Updated to correspond with the question edit:

var xml = XElement.Parse(yourString);
var allText = xml.Value;
var stripLeadingNumbers = Regex.Match(xml.Value, @"^\d*(.*)").Groups[1].Value;

First, and foremost, using regex to parse XML / HTML is bad .

Now, by error message I assume you mean the text, not including the numbers. An expression like so would probably do the trick:

\<([^>]+)\>&lt;!\[CDATA\[\d*(.*)\]\]&gt;\</\1\>

The error message will be in the second group. This will work with the sample that you have given, but I'd sooner use XDocument or XmlDocument to parse it. If you are using C#, there really isn't a good reason to not use either of those classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM