Solved

.net regular expressions

Posted on 2011-03-15
8
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 75

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 75

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 75

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
 
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 75

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

705 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