Forum Discussion

vikititor's avatar
vikititor
Contributor
5 years ago
Solved

how to write regex to detect end of line??

I am using simple script for reading the LOG file line by line. I need to detect end of line what is \r\n (because windows).

Now my LOG message ends with this: "bla bla bla.. status: OK, errors: {}"

 

So How to write regex in eachMatch method to decect, that in "{}" is nothing? In normal regex tester works all OK, but under ReadyAPI SoapUI not.. I am unable to use "\r\n" or "$" in my regex.. each time.. this fails.. 

I am unable to use "\{" and thenks to this, I need to write regex ends with "errors: .." 

BUT 

This not means, that in the end.. there will be some text in LOG file and I will be not detecting it.. Because I am unalbe to say, where is end of line.

 

Please help me.. I spend on this 3 hours.. testing outside, then in ReadyAPI, but What I am getting only is JAVA exceptions.. Error in scripts..

 

def processingFolderName = context.expand( '${#Project#absoluteImportUserFolderName}' ) + '\\processed'
def logFile = new FileNameByRegexFinder().getFileNames(processingFolderName, /.*output.csv/)
//log.info("log file: " + logFile[0].toString())

def f = new File(logFile[0].toString()) 
def records = f.readLines();
def String line
def founds = 0;
\

// need to detect, if there is no error on this row in LOG:
//"userName2,Password@1,mail@some.com2,Name2,Surname2,Location2,ENG,[ROLE_ADMIN]",SUCCESS,{}

records.each {
	line = it

line.eachMatch("userName2,Password@1,mail@some.com2,Name2,Surname2,Location2,ENG,.ROLE_ADMIN..,SUCCESS,..")
	{
		log.info( "Pattern 1 fonund on line: ")
		log.info( line )
		founds ++;
		}
}

log.info(founds)
if (founds < 3) {
	context.getTestRunner().fail("LOG pattern not found!")       
	}

return;
  • I've had to mess around with regex and ReadyAPI and escaping escape characters. It gets really tricky, but I can show you an example I've been working with: 

     

    Here is a standard regex pattern:

    [A-Za-z0-9\.,\(\)\-\+@\?:\s]*

    To make this work in ReadyAPI / groovy, I have to escape it to look like this:

    [A-Za-z0-9\\.,\\(\\)\\-\\+@\\?:\\s]

    I am taking a look at your example line and match pattern and will see if I can figure it out. Can you potentially provide a copy of the log file you are trying to parse? It'll be easier to work with what you have, or a mocked up file of what you need to process. That way I'm working with the closest thing possible to the source. 

  • YES.. !!!! :-D

    Why this simple help is not a part of your user guide? I did not found any information about escaping by "\\" nowhere.

    It works!

    I can write simple regex to detect quotes like "\\{\\}" and it matches.

    SO I KNOW, that in between { and } is nothing.. so no problem.

    I can write simple regex to check if end of line is present:

    line.eachMatch("SUCCESS,\\{\\}\\Z")

     

    Thanks a lot. 

    PS: please add this usefull thing in to your WIKI pages.. where your are explaining RegExp in groovy.. will be nice to know.

7 Replies

    • groovyguy's avatar
      groovyguy
      Champion Level 1

      I've had to mess around with regex and ReadyAPI and escaping escape characters. It gets really tricky, but I can show you an example I've been working with: 

       

      Here is a standard regex pattern:

      [A-Za-z0-9\.,\(\)\-\+@\?:\s]*

      To make this work in ReadyAPI / groovy, I have to escape it to look like this:

      [A-Za-z0-9\\.,\\(\\)\\-\\+@\\?:\\s]

      I am taking a look at your example line and match pattern and will see if I can figure it out. Can you potentially provide a copy of the log file you are trying to parse? It'll be easier to work with what you have, or a mocked up file of what you need to process. That way I'm working with the closest thing possible to the source. 

      • vikititor's avatar
        vikititor
        Contributor

        YES.. !!!! :-D

        Why this simple help is not a part of your user guide? I did not found any information about escaping by "\\" nowhere.

        It works!

        I can write simple regex to detect quotes like "\\{\\}" and it matches.

        SO I KNOW, that in between { and } is nothing.. so no problem.

        I can write simple regex to check if end of line is present:

        line.eachMatch("SUCCESS,\\{\\}\\Z")

         

        Thanks a lot. 

        PS: please add this usefull thing in to your WIKI pages.. where your are explaining RegExp in groovy.. will be nice to know.

  • richie's avatar
    richie
    Community Hero
    Hey vikititor,

    Im responding cos noone else has yet. I cant help with the groovy per se, but i can probably help with the regex.

    You state "in normal regex tester" your regex works but not in ReadyAPI!.
    can you explain what you mean by "doesnt work in ReadyAPI!"?
    Can you provide the logs for your regex attempts please?
    Are you saying youre attempting to escape the metacharacters (e.g. $, {, etc.) So they behave as literals but this isnt working?

    Cheers,

    Rich
    • vikititor's avatar
      vikititor
      Contributor

      My LOG message ends with literals "{}CrLf" I need to escape "{" and "}" and write some regex for end of line. 

      But write something like "\{\}\r\n" gives me exception in ReadyAPI..

       

      LOG message in file is:

      "userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,[ROLE_ADMIN, ROLE_USER]",SUCCESS,{}

       

      This works for me (but no idea, if there is an error text in "{some error}\r\n" because I am unable detect end of line..

      def f = new File(logFile[0].toString()) 
      def records = f.readLines();
      def String line
      def founds = 0;
      
      records.each {
      	line = it
      	line.eachMatch("userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,.*(ROLE)+.*(ROLE)+.*,SUCCESS..")
      	{
      		log.info( "Pattern 1 fonund on line: ")
      		log.info( line )
      		founds ++;
      		}

      When I update RegEx like this;

      line.eachMatch("userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,.*(ROLE)+.*(ROLE)+.*,SUCCESS,\{..")	

      I get exception in ReadAPI:


      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script4.groovy: 12: unexpected char: '\' @ line 12, column 112. ,.*(ROLE)+.*(ROLE)+.*,SUCCESS,\{..") ^ org.codehaus.groovy.syntax.SyntaxException: unexpected char: '\' @ line 12, column 112. at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:138) at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:111) at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:237) at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:167) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:694) at groovy.lang.GroovyShell.parse(GroovyShell.java:706) at groovy.lang.GroovyShell.parse(GroovyShell.java:742) at groovy.lang.GroovyShell.parse(GroovyShell.java:733) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.recompileWithNewName(SoapUIGroovyScriptEngine.java:151) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:142) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:91) at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SoapUIProGroovyScriptEngineFactory.java:83) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:158) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:331) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: Script4.groovy:12:112: unexpected char: '\' at org.codehaus.groovy.antlr.parser.GroovyLexer.mESC(GroovyLexer.java:2192) at org.codehaus.groovy.antlr.parser.GroovyLexer.mSTRING_CTOR_END(GroovyLexer.java:2238) at org.codehaus.groovy.antlr.parser.GroovyLexer.mSTRING_LITERAL(GroovyLexer.java:1997) at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:478) at org.codehaus.groovy.antlr.parser.GroovyLexer$1.nextToken(GroovyLexer.java:262) at groovyjarjarantlr.TokenBuffer.fill(TokenBuffer.java:69) at groovyjarjarantlr.TokenBuffer.LA(TokenBuffer.java:80) at groovyjarjarantlr.LLkParser.LA(LLkParser.java:52) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathExpression(GroovyRecognizer.java:11680) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.postfixExpression(GroovyRecognizer.java:13410) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.unaryExpressionNotPlusMinus(GroovyRecognizer.java:13379) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.powerExpressionNotPlusMinus(GroovyRecognizer.java:13083) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.multiplicativeExpression(GroovyRecognizer.java:13015) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.additiveExpression(GroovyRecognizer.java:12685) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.shiftExpression(GroovyRecognizer.java:9921) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.relationalExpression(GroovyRecognizer.java:12590) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.equalityExpression(GroovyRecognizer.java:12514) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.regexExpression(GroovyRecognizer.java:12462) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.andExpression(GroovyRecognizer.java:12430) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.exclusiveOrExpression(GroovyRecognizer.java:12398) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.inclusiveOrExpression(GroovyRecognizer.java:12366) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalAndExpression(GroovyRecognizer.java:12334) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalOrExpression(GroovyRecognizer.java:12302) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.conditionalExpression(GroovyRecognizer.java:4956) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.assignmentExpression(GroovyRecognizer.java:8122) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expression(GroovyRecognizer.java:10061) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatementNoCheck(GroovyRecognizer.java:8449) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatement(GroovyRecognizer.java:8995) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1278) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.blockBody(GroovyRecognizer.java:1587) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.closableBlock(GroovyRecognizer.java:8874) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.appendedBlock(GroovyRecognizer.java:11630) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathElement(GroovyRecognizer.java:11582) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.pathExpression(GroovyRecognizer.java:11697) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.postfixExpression(GroovyRecognizer.java:13410) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.unaryExpressionNotPlusMinus(GroovyRecognizer.java:13379) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.powerExpressionNotPlusMinus(GroovyRecognizer.java:13083) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.multiplicativeExpression(GroovyRecognizer.java:13015) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.additiveExpression(GroovyRecognizer.java:12685) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.shiftExpression(GroovyRecognizer.java:9921) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.relationalExpression(GroovyRecognizer.java:12590) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.equalityExpression(GroovyRecognizer.java:12514) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.regexExpression(GroovyRecognizer.java:12462) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.andExpression(GroovyRecognizer.java:12430) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.exclusiveOrExpression(GroovyRecognizer.java:12398) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.inclusiveOrExpression(GroovyRecognizer.java:12366) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalAndExpression(GroovyRecognizer.java:12334) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.logicalOrExpression(GroovyRecognizer.java:12302) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.conditionalExpression(GroovyRecognizer.java:4956) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.assignmentExpression(GroovyRecognizer.java:8122) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expression(GroovyRecognizer.java:10061) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatementNoCheck(GroovyRecognizer.java:8449) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatement(GroovyRecognizer.java:8995) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1278) at org.codehaus.groovy.antlr.parser.GroovyRecognizer.compilationUnit(GroovyRecognizer.java:760) at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:134) ... 22 more 1 error

       

      When I test this on web:

      https://regex101.com/

      use pattern:

      userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,.*(ROLE)+.*(ROLE)+.*,SUCCESS,\{\}

      text is:

      userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,[ROLE_ADMIN, ROLE_USER],SUCCESS,{}

      WORKS!!!! Nice, now I am able to say, that brackets {} are empty.. = no error.

       

      Because I am unable to check literal bracktes in ReadyAPI.. then I tryed to found out end of line using "\r" or "\n" in my groovy:

      line.eachMatch("userName1,Password@1,mail@some.com1,Name1,Surname1,Location1,eng,.*(ROLE)+.*(ROLE)+.*,SUCCESS,.*\n")

      BUT I was unable to fond end of lines.. WHY?????

    • vikititor's avatar
      vikititor
      Contributor

      I hope, this is with details.. 

      Simpy:

      I need to know, if inside of "{}" is nothing.. 

      OR

      I need to know, that after "SUCCESS,.." is end of line. Because I am unable to escape "{}" in RegEx.. then I was replaced them by "..". BUT, Problem is, that when some error is in there like "{some error}" the regex "SUCCESS,.." will pass.. MEANS.. this is positive FAILS..  In the end.. I dont know nothing..

      Nothing works under ReadyAPI..

       

      • vikititor's avatar
        vikititor
        Contributor

        No solution? Some strangth behaviour of ReadyAPI? or my mistake?