Solved

.net regular expressions

Posted on 2011-03-15
8
362 Views
Last Modified: 2012-06-27
I am trying to use regular expressions to pull the value inside of <STATUS> I was trying to use the code below but it is not returning a result.

<STATUS>Lorem</STATUS>
re = New Regex("(?<=<status>)[^</status>]*", RegexOptions.IgnoreCase)

Open in new window

0
Comment
Question by:jimseiwert
  • 4
  • 3
8 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35141652
That's because the constrct [ ... ] looks at each character within the brackets, not the phrase or string as whole. What you said above was, "not any character that is <, /, s, t, a, u, or >". What I believe you are after would be more along the lines of:
re = New Regex("(?<=<status>)(?:[^<]|<(?!/status))*", RegexOptions.IgnoreCase)

Open in new window

0
 
LVL 2

Author Comment

by:jimseiwert
ID: 35141684
That was it. Can you explain what each piece means in this (?<=<status>)(?:[^<]|<(?!/status))*  so I can learn for future use?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35141697
>>  That's because the constrct [ ... ] looks at each character

That's of course excluding the ^ at the beginning of the expression since that makes it a "NOT" expression  = )
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35141731
>>  Can you explain what each piece means in this (?<=<status>)(?:[^<]|<(?!/status))*  so I can learn for future use?

Sure  :  )

(?<=<status>)

Open in new window

Positive lookbehind. Look backwards from the current location and try to find the string "<status>"



(?: ... )*

Open in new window

Non-capturing group modified with *. Find zero-or-more of the thing the group described.



[^<]|<(?!/status)

Open in new window

Find either any character not an opening chevron OR find an opening chevron that is not followed by the string "/status". The construct (?! ... ) is a negative lookahead, meaning the match succeeds if the string described by the lookahead is NOT found.


I have an article describing lookaround (lookbehind and lookahead) which you may find useful:  http://www.experts-exchange.com/A_4318.html
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35141732
Kaufmed,

Couldn't that be simplified slightly? since < isn't valid in HTML (or XML, if I'm correct, would need to be encoded as &lt; in both cases):

New Regex("<status>([^<]*)</status>", RegexOptions.IgnoreCase)
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35141742
No, nevermind - that wouldn't make sense. ;)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35141759
@tgerbert

>>  Couldn't that be simplified slightly? since < isn't valid in HTML

I agree, and I originally was going to post something like that, but I figured on the off-chance the author had a tag in between (since I don't know what the source data looks like), I took the long-winded approach  = )
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35141770
Yeah, embedded tags crossed mind just a split second after I clicked the Post button.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now