Solved

.net regular expressions

Posted on 2011-03-15
8
366 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host asp.net pages 5 25
Not showing JavaScript in the list 5 40
Greetings C# program 17 57
Point to Current Row Ater Refresh Datagridview 3 21
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

896 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

11 Experts available now in Live!

Get 1:1 Help Now