Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.net Regular Expression

Status
Not open for further replies.

0ddball

Technical User
Nov 6, 2003
208
GB
I'm trying to build a regular expression to work with the .NET Regex object. The expression is designed to parse a forum tag such as [foo bar="foo"] - it should also recognise attributeless tags [foo] and self closing tags [foo /] or [foo bar="foo" /]

Here is what I have so far:
Code:
\[((?=[^/]).*?)(?(?=\s\w+=".+?")(?:\s(\w+)="(.+?)")*(?:\s(/))?\]|(?:\s(/))?\])

For some reason this fails to capture the group represented by (\w+) - the pattern matches but completly fails to capture that group. It's weird.

If I take out the conditional - that's the bit that starts (?(?= - then it matches the pattern correctly for tags which have attributes (see below)

Code:
\[((?=[^/]).*?)(?=\s\w+=".+?")(?:\s(\w+)="(.+?)")*(?:\s(/))?\]

If the damn thing would capture groups it's supposed to I wouldn't be having this problem :D

Any ideas?


Yet another unchecked rambling brought to you by:
Oddball
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top