javajoemorgan Posted March 4, 2011 Share Posted March 4, 2011 I have worked on this for hours, and cannot come up with how to do it. I need a regular expression that matches a string containing a dash that is not in any way contained in square brackets. Here are some examples of the rules:// Should matchsomething - somethingElse // Should NOT matchsomething[ 4 - 6 ]// Should matchsomething[4-6] - somethingElse// Should matchsomething - somethingElse[ 3 - 5]// Should matchsomething[4 - 6] - somethingElse[1-3]// Should NOT matchsomething[3, 4, 5-6, 7]// Should matchsomething[3, 4, 5-6, 7] - somethingElse[3, 4-5]I was thinking that if I could match what is in brackets, I could maybe figure out the negative... but I've been thinking on it so long now I'm just lost. Link to comment Share on other sites More sharing options...
Martin Honnen Posted March 4, 2011 Share Posted March 4, 2011 At least for your samples the following work: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output indent="yes"/> <xsl:variable name="re" as="xs:string">^([^\[\]]*)(\[.*?\])*([^\[\]]*)\-([^\[\]]*)(\[.*?\])*([^\[\]]*)$</xsl:variable> <xsl:template match="data[matches(., $re)]"> <matched> <xsl:value-of select="."/> </matched> </xsl:template> <xsl:template match="data[not(matches(., $re))]"> <not-matched> <xsl:value-of select="."/> </not-matched> </xsl:template> </xsl:stylesheet> When applied to the input <list> <data>something - somethingElse</data> <data>something[ 4 - 6 ]</data> <data>something[4-6] - somethingElse</data> <data>something - somethingElse[ 3 - 5]</data> <data>something[4 - 6] - somethingElse[1-3]</data> <data>something[3, 4, 5-6, 7]</data> <data>something[3, 4, 5-6, 7] - somethingElse[3, 4-5]</data></list> Saxon 9.3 outputs <matched>something - somethingElse</matched> <not-matched>something[ 4 - 6 ]</not-matched> <matched>something[4-6] - somethingElse</matched> <matched>something - somethingElse[ 3 - 5]</matched> <matched>something[4 - 6] - somethingElse[1-3]</matched> <not-matched>something[3, 4, 5-6, 7]</not-matched> <matched>something[3, 4, 5-6, 7] - somethingElse[3, 4-5]</matched> Link to comment Share on other sites More sharing options...
javajoemorgan Posted March 5, 2011 Author Share Posted March 5, 2011 No wonder my head hurt so much.... I had a significant subset of it, but was definitely missing the beginning/ending bracket exclusions.. .I just had the beginning ([^\[]*) ... ([^\]]) followed by the ending... I'm still absorbing how that works... but thanks... At least for your samples the following work:... <xsl:variable name="re" as="xs:string">^([^\[\]]*)(\[.*?\])*([^\[\]]*)\-([^\[\]]*)(\[.*?\])*([^\[\]]*)$</xsl:variable> ... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.