Regex Pattern for Ascii Tree Parsing (Maven/Gradle Dependency output)

369 Views Asked by At

I am attempting to build a comprehensive regex pattern to match all the an ascii tree , which is an outcome of mvn/gradle dependency representation

Note that the pattern should address the following order options

+- 
|    +---
|    |    +---
|    |    \---
\---
     \---

This regex pattern matches but does not take ordering into consideration. Would appreciate some advice in this matter

(\|)|(\\-*)|(\+-*)

A sample data file is specified below

+- com.acme.org:commons-integration:jar:2.39.0:compile
|  +- (com.acme.org:commons-core:jar:2.39.0:compile - omitted for duplicate)
|  +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
|  |  \- (commons-logging:commons-logging:jar:1.0.3:compile - omitted for conflict with 1.1)
|  +- c3p0:c3p0:jar:0.9.1:compile
|  +- (org.springframework:spring:jar:2.5.6:compile - omitted for duplicate)
|  +- javax.persistence:persistence-api:jar:1.0:compile
|  +- (org.springframework.batch:spring-batch-core:jar:2.0.0.RELEASE:compile - omitted for duplicate)
|  \- org.springframework:spring-tx:jar:2.5.5:compile
|     +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.1)
|     +- (org.springframework:spring-beans:jar:2.5.5:compile - omitted for conflict with 2.5.6)
|     +- (org.springframework:spring-context:jar:2.5.5:compile - omitted for conflict with 2.5.6)
|     \- (org.springframework:spring-core:jar:2.5.5:compile - omitted for conflict with 2.5.6)
+- active project artifact:
    artifact = com.acme.org:foo-core-impl:jar:1.0.41-SNAPSHOT:compile;
    project: MavenProject: com.acme.org:foo-core-impl:1.0.41-SNAPSHOT @ /opt/jenkins/home/jobs/foo/workspace/trunk/foo-core-impl/pom.xml
|  +- active project artifact:
    artifact = com.acme.org:foo-core:jar:1.0.41-SNAPSHOT:compile;
    project: MavenProject: com.acme.org:foo-core:1.0.41-SNAPSHOT @ /opt/jenkins/home/jobs/foo/workspace/trunk/foo-core/pom.xml
|  |  \- (com.acme.org:commons-core:jar:2.39.0:compile - omitted for duplicate)
|  +- (com.acme.org:commons-core:jar:2.39.0:compile - omitted for duplicate)
|  +- (org.springframework:spring:jar:2.5.6:compile - omitted for duplicate)
|  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  +- (commons-collections:commons-collections:jar:3.2:compile - omitted for duplicate)
|  \- com.acme.org:referentials-client:jar:2.11.11:compile
|     +- com.acme.org:referentials-core:jar:2.11.10:compile
|     |  +- (com.acme.org:commons-client:jar:2.40.1:compile - omitted for duplicate)
|     |  \- (commons-collections:commons-collections:jar:3.2:compile - omitted for duplicate)
|     \- (com.acme.org:commons-client:jar:2.40.1:compile - omitted for conflict with 2.39.0)
+- com.acme.org:commons-core:jar:2.39.0:compile
|  +- commons-dbutils:commons-dbutils:jar:1.2:compile
|  +- oro:oro:jar:2.0.8:compile
|  +- javax.xml.bind:jaxb-api:jar:2.1:compile (version managed from 2.0)
|  |  +- javax.xml.stream:stax-api:jar:1.0-2:compile
|  |  \- javax.activation:activation:jar:1.1:compile
|  +- com.sun.xml.bind:jaxb-impl:jar:2.0.3:compile (version managed from 2.1.3)
|  |  +- (javax.xml.bind:jaxb-api:jar:2.1:compile - version managed from 2.0; omitted for duplicate)
|  |  +- javax.xml.bind:jsr173_api:jar:1.0:compile
|  |  \- (javax.activation:activation:jar:1.1:compile - omitted for duplicate)
|  +- (org.springframework:spring:jar:2.5.6:compile - omitted for duplicate)
|  +- (commons-codec:commons-codec:jar:1.4:compile - omitted for duplicate)
|  +- org.springmodules:spring-modules-cache:jar:0.8:compile
|  +- cglib:cglib-nodep:jar:2.1_3:compile
|  +- commons-dbcp:commons-dbcp:jar:1.2.2:compile
|  |  \- commons-pool:commons-pool:jar:1.3:compile
|  +- net.sf.ehcache:ehcache:jar:1.2.4:compile
|  |  \- (commons-logging:commons-logging:jar:1.0.4:compile - omitted for conflict with 1.1.1)
|  +- (commons-collections:commons-collections:jar:3.2:compile - omitted for duplicate)
|  +- commons-cli:commons-cli:jar:1.0:compile
|  |  +- (commons-logging:commons-logging:jar:1.0:compile - omitted for conflict with 1.1.1)
|  |  \- (commons-lang:commons-lang:jar:1.0:compile - omitted for conflict with 2.5)
|  +- (commons-beanutils:commons-beanutils:jar:1.7.0:compile - omitted for duplicate)
|  +- (commons-lang:commons-lang:jar:2.5:compile - omitted for duplicate)
|  +- (c3p0:c3p0:jar:0.9.1.2:compile - omitted for conflict with 0.9.1)
|  +- commons-httpclient:commons-httpclient:jar:3.0.1:compile
|  |  +- (junit:junit:jar:4.4:compile - version managed from 3.8.1; omitted for duplicate)
|  |  +- (commons-logging:commons-logging:jar:1.0.3:compile - omitted for conflict with 1.1.1)
|  |  \- (commons-codec:commons-codec:jar:1.2:compile - omitted for conflict with 1.4)
|  +- log4j:log4j:jar:1.2.15:compile
|  |  +- javax.mail:mail:jar:1.4:compile
|  |  |  \- (javax.activation:activation:jar:1.1:compile - omitted for duplicate)
|  |  +- javax.jms:jms:jar:1.1:compile
|  |  +- com.sun.jdmk:jmxtools:jar:1.2.1:compile
|  |  \- com.sun.jmx:jmxri:jar:1.2.1:compile
|  +- xerces:xercesImpl:jar:2.8.1:compile
|  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
|  +- (commons-logging:commons-logging:jar:1.1:compile - omitted for conflict with 1.1.1)
|  +- commons-logging:commons-logging-api:jar:1.1:compile
|  \- com.itextpdf:itextpdf:jar:5.0.5:compile
|     +- org.bouncycastle:bcmail-jdk14:jar:1.38:compile
|     |  \- (org.bouncycastle:bcprov-jdk14:jar:1.38:compile - omitted for duplicate)
|     +- org.bouncycastle:bcprov-jdk14:jar:1.38:compile
|     \- org.bouncycastle:bctsp-jdk14:jar:1.38:compile
|        +- (org.bouncycastle:bcprov-jdk14:jar:1.38:compile - omitted for duplicate)
|        \- (org.bouncycastle:bcmail-jdk14:jar:1.38:compile - omitted for duplicate)
+- org.hibernate:hibernate-validator:jar:4.1.0.Final:compile
|  +- (javax.validation:validation-api:jar:1.0.0.GA:compile - omitted for duplicate)
|  \- org.slf4j:slf4j-api:jar:1.5.6:compile
+- commons-logging:commons-logging:jar:1.1.1:compile
+- commons-lang:commons-lang:jar:2.5:compile
+- commons-codec:commons-codec:jar:1.4:compile
+- commons-collections:commons-collections:jar:3.2:compile
+- org.springframework:spring:jar:2.5.6:compile
|  \- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
+- org.springframework.batch:spring-batch-core:jar:2.0.0.RELEASE:compile
|  +- (org.springframework.batch:spring-batch-infrastructure:jar:2.0.0.RELEASE:compile - omitted for duplicate)
|  +- (commons-lang:commons-lang:jar:2.1:compile - omitted for conflict with 2.5)
|  +- org.aspectj:aspectjrt:jar:1.5.4:compile
|  +- org.aspectj:aspectjweaver:jar:1.5.4:compile
|  +- com.thoughtworks.xstream:xstream:jar:1.3:compile
|  |  \- xpp3:xpp3_min:jar:1.1.4c:compile
|  +- org.codehaus.jettison:jettison:jar:1.0:compile
|  +- org.springframework:spring-aop:jar:2.5.6:compile
|  |  +- aopalliance:aopalliance:jar:1.0:compile
|  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-beans:jar:2.5.6:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-core:jar:2.5.6:compile - omitted for duplicate)
|  +- org.springframework:spring-beans:jar:2.5.6:compile
|  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-core:jar:2.5.6:compile - omitted for duplicate)
|  +- org.springframework:spring-context:jar:2.5.6:compile
|  |  +- (aopalliance:aopalliance:jar:1.0:compile - omitted for duplicate)
|  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  |  +- (org.springframework:spring-beans:jar:2.5.6:compile - omitted for duplicate)
|  |  \- (org.springframework:spring-core:jar:2.5.6:compile - omitted for duplicate)
|  +- org.springframework:spring-core:jar:2.5.6:compile
|  |  \- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  +- (org.springframework:spring-tx:jar:2.5.6:compile - omitted for conflict with 2.5.5)
|  \- stax:stax:jar:1.2.0:compile
+- org.springframework.batch:spring-batch-infrastructure:jar:2.0.0.RELEASE:compile
|  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for duplicate)
|  +- (org.springframework:spring-core:jar:2.5.6:compile - omitted for duplicate)
|  \- (stax:stax:jar:1.2.0:compile - omitted for duplicate)
+- javax.validation:validation-api:jar:1.0.0.GA:compile
+- com.acme.org:commons-client:jar:2.39.0:runtime (scope not updated to compile)
|  +- (com.acme.org:commons-core:jar:2.39.0:runtime - omitted for duplicate)
|  +- javax.transaction:jta:jar:1.0.1B:runtime
|  +- org.apache.activemq:activemq-core:jar:5.1.0:runtime
|  |  +- (commons-logging:commons-logging-api:jar:1.1:runtime - omitted for duplicate)
|  |  +- org.apache.camel:camel-core:jar:1.3.0:runtime
|  |  |  +- (commons-logging:commons-logging-api:jar:1.0.4:runtime - omitted for conflict with 1.1)
|  |  |  +- (javax.xml.bind:jaxb-api:jar:2.1:runtime - version managed from 2.0; omitted for duplicate)
|  |  |  \- (com.sun.xml.bind:jaxb-impl:jar:2.0.3:runtime - version managed from 2.1.3; omitted for duplicate)
|  |  +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:runtime
|  |  \- org.apache.geronimo.specs:geronimo-j2ee-management_1.0_spec:jar:1.0:runtime
|  +- org.apache.activemq:activeio-core:jar:3.1.0:runtime
|  |  +- (commons-logging:commons-logging:jar:1.1:runtime - omitted for conflict with 1.1.1)
|  |  \- backport-util-concurrent:backport-util-concurrent:jar:2.1:runtime
|  +- com.ibm:mqjms:jar:6.0.0:runtime
|  +- com.ibm:mq:jar:6.0.0:runtime
|  +- com.ibm:connector:jar:6.0.0:runtime
|  +- com.ibm:jta:jar:6.0.0:runtime
|  \- com.ibm:dhbcore:jar:6.0.0:runtime
+- com.oracle:ojdbc5:jar:11.2.0.1.0:runtime
+- org.slf4j:slf4j-log4j12:jar:1.5.6:runtime
|  \- (org.slf4j:slf4j-api:jar:1.5.6:runtime - omitted for duplicate)
+- com.acme.org:commons-test:jar:2.26.8:test
|  +- httpunit:httpunit:jar:1.7:test
|  |  +- javax.servlet:servlet-api:jar:2.3:test
|  |  \- jtidy:jtidy:jar:4aug2000r7-dev:test
|  +- (org.springframework:spring:jar:2.5.6:test - omitted for duplicate)
|  +- org.springframework:spring-test:jar:2.5.6:test
|  |  +- (commons-logging:commons-logging:jar:1.1.1:test - omitted for duplicate)
|  |  \- (junit:junit:jar:4.4:test - version managed from 3.8.1; omitted for duplicate)
|  +- org.mortbay.jetty:jetty:jar:6.1.24:test
|  |  +- org.mortbay.jetty:jetty-util:jar:6.1.24:test
|  |  \- org.mortbay.jetty:servlet-api:jar:2.5-20081211:test
|  +- (xml-apis:xml-apis:jar:1.0.b2:compile - scope updated from test; omitted for duplicate)
|  +- jdom:jdom:jar:1.0:test
|  +- (xerces:xercesImpl:jar:2.8.1:test - omitted for duplicate)
|  +- (javax.xml.bind:jaxb-api:jar:2.1:test - version managed from 2.2.1; omitted for duplicate)
|  +- (com.sun.xml.bind:jaxb-impl:jar:2.0.3:test - version managed from 2.2.1; omitted for duplicate)
|  \- (commons-httpclient:commons-httpclient:jar:3.0.1:test - omitted for duplicate)
+- org.mockito:mockito-all:jar:1.8.5:test
+- org.powermock:powermock-api-mockito:jar:1.4.7:test
|  +- (org.mockito:mockito-all:jar:1.8.5:test - omitted for duplicate)
|  \- org.powermock:powermock-api-support:jar:1.4.7:test
|     +- org.powermock:powermock-core:jar:1.4.7:test
|     |  +- (org.powermock:powermock-reflect:jar:1.4.7:test - omitted for duplicate)
|     |  \- org.javassist:javassist:jar:3.14.0-GA:test
|     \- org.powermock:powermock-reflect:jar:1.4.7:test
|        \- org.objenesis:objenesis:jar:1.2:test
\- junit:junit:jar:4.4:test (scope not updated to compile)


+- javax.activation:activation:jar:1.1.1:compile
+- com.h2database:h2:jar:1.4.190:compile
+- axis:axis:jar:1.4:compile
|  \- axis:axis-wsdl4j:jar:1.5.1:runtime
+- org.apache.axis:axis-saaj:jar:1.4:compile
+- org.apache.axis:axis-jaxrpc:jar:1.4:compile
+- org.apache.axis:axis-ant:jar:1.4:compile
+- org.apache.commons:commons-lang3:jar:3.4:compile
+- commons-io:commons-io:jar:2.4:compile
+- commons-collections:commons-collections:jar:3.2.1:compile
+- commons-digester:commons-digester:jar:2.1:compile
|  \- commons-beanutils:commons-beanutils:jar:1.8.3:compile
+- commons-logging:commons-logging:jar:1.2:compile
+- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
+- commons-discovery:commons-discovery:jar:0.5:compile
+- javax.mail:javax.mail-api:jar:1.5.4:compile
+- hsqldb:hsqldb:jar:1.8.0.10:compile
+- wsdl4j:wsdl4j:jar:1.6.3:compile
+- java2html:j2h:jar:1.3.1:compile
+- ecs:ecs:jar:1.4.2:compile
+- javax.transaction:javax.transaction-api:jar:1.2:compile
+- net.sourceforge.jtds:jtds:jar:1.3.1:compile
+- org.apache.tomcat:tomcat-catalina:jar:7.0.65:provided


+--- project :hollow
\--- project :hollow-ui-tools
     +--- project :hollow
     +--- org.apache.velocity:velocity:1.7
     |    +--- commons-collections:commons-collections:3.2.1
     |    \--- commons-lang:commons-lang:2.4
     +--- commons-codec:commons-codec:1.8
     +--- commons-io:commons-io:2.3
     \--- com.google.code.gson:gson:2.8.0
1

There are 1 best solutions below

5
On BEST ANSWER

How about this:

^ *((\| +)*\+-+|(\| +)*\\-+)

I match the start of a line followed by zero or more spaces then the pipe with trailing spaces zero or many times followed by a plus and a dash one or many times OR a pipe with trailing spaces zero or many times followed by a backlash and a dash one or many times.

That covers all the patterns I can see in both your test case and your actual maven output.