RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^(?<path>(/?(?<step>\w+))+)(?<predicate>\[(?<comparison>\s*(?<lhs>@\w+)\s*(?<operator><=|>=|<>|=|<|>)\s*(?<rhs>('[^']*'|"[^"]*"))\s*(and|or)?)+\])*$
Description
Simple parser of XPath, it doesn't handle the more complex statements but can be of use in some situations.
Matches
/root/child1/child2[ @param='value' ] | child1[@param >= "1" and @foo < 'asdf"
Non-Matches
/root/*[ local-name() = 'child1' ][ child2/@param='value ]/child2
Author Rating: Not yet rated. Tim Walters
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: XMP Path
Name: Validation
Date: 6/25/2008 6:58:15 AM
Comment:
Very bad regular expression dur to performance point of view


Title: Some nice stuff there...
Name: Darren Neimke
Date: 1/23/2004 4:50:30 AM
Comment:
Tim, I like the pattern as most of it seems quite "tight". Just a couple of things... it would match: /boo[ @foo <> 'bar' and] - is that correct? It wouldn't match against this: /boo[ @foo <> 'bar' and ] - (note the space after the "and" operator), but, it would probably force quite a few backtrack attempts before failing. I'm not sure how many but, I reckon that there'd be a few. You could also save yourself 3 captured groups by turning on ExplicitCapture (?n) like so: (?n) ^ (?<path> (/? (?<step>\w+) )+ ) (?<predicate> \[ (?<comparison> \s* (?<lhs>@\w+) \s* (?<operator><=|>=|<>|=|<|>) \s* (?<rhs> ('[^']*'|"[^"]*") ) \s* (and|or)? )+ \] )* $


Copyright © 2001-2025, RegexAdvice.com | ASP.NET Tutorials