0

I have a regular expression that I use to grab data between two sets of id's for example <CLASSCOD>70</CLASSCOD> The regular expression I use is (?<=<CLASSCOD>)(?:[^<]|<(?!/CLASSCOD))* which works in most case but when i have a single value like this <CLASSCOD>N</CLASSCOD> it says there are no matches.

The whole data string looks like this

<DATE>0601</DATE>
<YEAR>11</YEAR>
<AGENCY>Department of the Interior</AGENCY>
<OFFICE>Bureau of Indian Affairs</OFFICE>
<LOCATION>BIA - DAPM</LOCATION>
<ZIP>85004</ZIP>
<CLASSCOD>N</CLASSCOD>
<OFFADD>Contracting Office - Western Region 2600 N. Central Avenue, 4th Floor Phoenix AZ 85004</OFFADD>
<SUBJECT>Boiler Replacement</SUBJECT>
<SOLNBR>A11PS00463</SOLNBR>
<RESPDATE>061711</RESPDATE>
<ARCHDATE>05312012</ARCHDATE>
<CONTACT>Geraldine M. Williams Purchasing Agent 6023794087 [email protected];<a href="mailto:[email protected]">Point of Contact above, or if none listed, contact the IDEAS EC HELP DESK for assistance</a>
</CONTACT>
<LINK><URL>https://www.fbo.gov/spg/DOI/BIA/RestonVA/A11PS00463/listing.html<LINKDESC>Link To Document</LINK>
<EMAIL></EMAIL>
<EMAIL>
  [email protected]
  <EMAILDESC>
    Point of Contact above, or if none listed, contact the IDEAS EC HELP DESK for assistance
  </EMAILDESC>
</EMAIL>
<SETASIDE>Total Small Business</SETASIDE>
<POPCOUNTRY>USA</POPCOUNTRY>
<POPZIP>85634</POPZIP>
<POPADDRESS>BIE Tohono O'odham High School, Sells, AZ</POPADDRESS>

Any Suggestions as to the reason?

Thanks

3
  • 1
    I would suggest using XPath or some other XML technology than using regular expressions. Commented Jun 3, 2011 at 23:14
  • 1
    I just tried your regex against the example that you said didn't work, and it returned a match. Commented Jun 3, 2011 at 23:19
  • against the 70 or the N value? Commented Jun 3, 2011 at 23:22

2 Answers 2

2

Something simpler should work:

<CLASSCOD>(.+?)</CLASSCOD>

Example:

Match match = Regex.Match(input, @"<CLASSCOD>(.+?)</CLASSCOD>");
if (match.Success) {
    string value = match.Groups[1].Value;
    Console.WriteLine(value);
}
Sign up to request clarification or add additional context in comments.

2 Comments

That gives me my whole string back so for example "<CLASSCOD>N</CLASSCOD>" return "<CLASSCOD>N</CLASSCOD>" when all I want back is the value in between, In this case "N" other cases 70 sometimes more
Very Nice, I see what you mean.
1

If you would like to extract the value inside the brackets you may use the following RegEx:

<([^>]+)>([^<]*)</\1>

For this scenario there is no need to use the lookahead and lookbehind operators.

1 Comment

I updated the question. There is more then just this one bracket of data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.