-1

Okay guys

here are my 2 strings D:/path/to/project/js/folder/LVL1/LVL2/mylibrary.js and D:/path/to/project/js/folder/mylibrary.js

for the first I want to get /LVL1/LVL2 and for the second i want to get a slash /

here is my regex ^.+js\/folder(\/[^\.]*[^\/])\/.+\.js+$

it works fine for the first but returns nothing for the second

this is to be used in an ant task (more specifically the jscomp). Basically I want to compile every javascript file in the 'D:/path/to/project/js/folder/' and put the output somewhere else with the nested folders. Here's an excerpt of my ant task if it ever helps.

<target name="compileJS" description="minifier les fichier JS">
    <taskdef name="jscomp"
             classname="com.google.javascript.jscomp.ant.CompileTask"
             classpath="${lib.dir}/thirdparty/closure-compiler/compiler.jar" />
    <taskdef resource="net/sf/antcontrib/cpptasks/antlib.xml">
        <classpath>
            <pathelement location="${lib.dir}/thirdparty/ant-contrib/cpptasks.jar" />
        </classpath>
    </taskdef>

    <echo message="Searching for files in ${basedir}\charte\js" />
    <for param="filename">
        <path>
            <fileset dir="${basedir}/charte/js">
            </fileset>
        </path>
        <sequential id="i">
            <!-- This gets the filename without the directory path. -->
            <basename property="file.@{filename}" file="@{filename}" />


            <propertyregex property="file"
                           input="@{filename}"
                           regexp="\\"
                           replace="/"
                           override="true"
                           defaultvalue="@{filename}" />

            <echo message=">...>...> input = ${file}"/>

            <propertyregex property="directory.@{filename}"
                input="${file}"
                regexp="^.+js\/folder(\/[^\.]*[^\/])\/.+[\.js]+$"
                select="\1"
                casesensitive="false" override="true" />
            <echo message=">...>...> directory = ${directory.@{filename}}"/>

            <echo message="Compressing file ${file.@{filename}} in ${directory.@{filename}}" />

            <jscomp  compilationLevel="simple"
                output="${build.dir}/charte/js/directory.@{filename}/${file.@{filename}}">

                <sources dir="${base.dir}/charte/js/directory.@{filename}/">
                    <file name="${file.@{filename}}" />
                </sources>

            </jscomp>


        </sequential>
    </for>
</target>

Thanks

Nani
  • 317
  • 5
  • 15

2 Answers2

0

you could use this pattern

^.+js\/folder(\/|.*?)[^\/]+?\.js$  

Demo

^               # Start of string/line
.               # Any character except line break
+               # (one or more)(greedy)
js              # "js"
\/              # "/"
folder          # "folder"
(               # Capturing Group (1)
  \/            # "/"
  |             # OR
  .             # Any character except line break
  *?            # (zero or more)(lazy)
)               # End of # Capturing Group (1)
[^\/]           # Character not in [^\/]
+?              # (one or more)(lazy)
\.              # "."
js$             # End of string/line
alpha bravo
  • 7,838
  • 1
  • 19
  • 23
0

Don't have ant available. Relied on RegEx101.
Your regex seems to capture the same as a shorter version: ^.+js\/folder(\/[^\.]*)\/.+\.js+$
Neither one does (in RegEx101) match the second string, though. RegEx101

Restructuring the shorter version slightly ^.+js\/folder(\/)([^\.]*\/)*.+\.js+$ matches both strings, and returns the expected, if two back references are being used:
RegEx101

If ant requires adjustments, please, comment.

Abecee
  • 2,365
  • 2
  • 12
  • 20