Clarity

 View Only
  • 1.  GEL Script regular expression

    Posted Jul 17, 2020 05:43 AM
    Hi,

    I try to check a variable, if it is 3 digits.
    But I cannot get regex to work in GEL script.
    Is it possible or what is the correct syntax?

    I have included: xmlns:regexp="jelly:regexp"
    Don't know if this is correct or needed

    And when I try something like this (tried several syntax and masks things already)

    <core:if test="${variableA.match(/^\d\d\d$/g)}"
    or even
    <core:if test="${variableA.match(\d\d\d)}"

    I get an error "Unable to create expression". Or the if is never true.

    Does somebody used regex in GEL scripts and can provide an example? I also found some older posts, that this is not possible.


    ------------------------------
    Thanks and regards
    Christoph
    ------------------------------


  • 2.  RE: GEL Script regular expression

    Posted Jul 18, 2020 02:08 PM
    I am not sure the regex jelly tag lib is available.

    Here is an alternative way.

    <gel:script xmlns:core="jelly:core"
    	xmlns:regexp="jelly:regexp"
    	xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
    	xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    	xmlns:sql="jelly:sql" xmlns:xog="http://www.niku.com/xog"
    	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:xsl="http://www.w3.org/1999/XML/Transform"
    	xmlns:util="jelly:util">
    
    	<core:set var="testString" value="Hello World"/>
    	<gel:log message="Test matches on a string object = ${testString}" />
    
    	<core:set var="testResult" value='${testString.matches("^\d\d\d")}' />
    	<gel:log message="Test regexp = ${testResult}" />
    
    	<core:set var="testString" value="123"/>
    		<gel:log message="Test matches on a string object = ${testString}" />
    	<core:set var="testResult" value='${testString.matches("^\d\d\d")}' />
    	<gel:log message="Test regexp = ${testResult}" />
    
    	<gel:log message="Test innerOneException" />
    
    	<gel:log message="Test innerOneException with an integer" />
    	<core:set var="testString" value="      145     "/>
    	<core:catch var="innerOneException">
    		<core:invokeStatic className="java.lang.Integer" method="valueOf" var="intValue">  
    			<core:arg value="${testString.trim()}" />  
    		</core:invokeStatic> 
    	</core:catch>
    
    	<core:if test="${innerOneException == null}">
    		<gel:log message="We didn't get an innerOneException" />
    		<gel:log message="testString is an integer = ${testString.trim()}" />
    	</core:if>
    
    	<gel:log message="Test innerOneException with an none integer" />
    	<core:set var="testString" value="      A145A    "/>
    
    	<core:catch var="innerOneException">
    		<core:invokeStatic className="java.lang.Integer" method="valueOf" var="intValue">  
    			<core:arg value="${testString.trim()}" />  
    		</core:invokeStatic> 
    	</core:catch>
    
    	<core:if test="${innerOneException != null}">
    		<gel:log message="We did get an innerOneException"  />
    		<gel:log message="testString is not integer = ${testString.trim()}" />
    	</core:if>
    
    </gel:script>

    Test Results






  • 3.  RE: GEL Script regular expression
    Best Answer

    Posted Jul 18, 2020 05:58 PM
    Edited by Christopher Hackett Jul 24, 2020 01:20 PM
    The regexp taglib isn't included, only a small subset of taglibs are:

    R:\clarity\15.7.0\lib>dir common*tag*
    Volume in drive R is REGO
    Volume Serial Number is 560E-45EC

    Directory of R:\clarity\15.7.0\lib

    08/27/2019 06:05 AM 9,503 commons-jelly-tags-email.jar
    08/27/2019 06:05 AM 22,479 commons-jelly-tags-fmt.jar
    08/27/2019 06:05 AM 18,042 commons-jelly-tags-jsl.jar
    08/27/2019 06:05 AM 11,196 commons-jelly-tags-log.jar
    08/27/2019 06:05 AM 32,514 commons-jelly-tags-sql.jar
    08/27/2019 06:05 AM 16,888 commons-jelly-tags-util.jar
    08/27/2019 06:05 AM 37,430 commons-jelly-tags-xml.jar
    7 File(s) 148,052 bytes
    0 Dir(s) 509,890,560 bytes free

    R:\clarity\15.7.0\lib>

    (Plus "core", "gel", and "bpm" for the most part).

    Incidentally, just including the namespace entry into the <gel:script> tag doesn't actually use the taglib anywhere; you would then later on have to use a tag from it like <regexp:blahblah>, at which point you would then get an error telling you the classes for the taglib don't exist.

    When you're doing things in an expression like ${...} you're using JEXL (v1.1)** instead.

    JEXL has the ability to call java methods on its objects/variables, and so Gene's .matches() example will work because that's literally just calling the matches method of java.lang.string :

    https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-

    You can also, of course, use the JEXL to do the matching directly in the <core:if test="..." /> tag and don't have to do a <core:set> first, but I won't replicate the answer for that because Gene's is already adequate.


    ** Note that the available documentation for JEXL references using pattern matching expressions directly with =~ and !~ and other operators, but as far as I know these don't work as they were added in JEXL 3.x which Clarity doesn't use.  So be careful with what the available documentation claims you can / can't do because finding 1.1 docs is a challenge.  Apart from that, this is still a good reference and as above, you don't need to add ANY namespaces to use JEXL it's already present: http://commons.apache.org/proper/commons-jexl/reference/syntax.html