0

I have folder names with convention as below:

 org.apache.commons.httpclient_3.1.0.v20170718_1537

 org.apache.commons.httpclient_3.1.0.v20170718-1537

 org.apache.james.mime4j_0.6.0.v20120423

In these I wanted to separate the names alone without the version names. (**Example:**org.apache.commons.httpclient_3.1.0.v20170718_1537 to be mentioned as org.apache.commons.httpclient)

               <propertyregex property="MyRegex"
                           input="@{MyFolder}"
                           regexp="_(.*)"
                           select="\0"
                           casesensitive="false" />

I wanted to separate the name alone using ANT. So I have tried regex like "_(.*)" (I have tried with some other regex expressions also, that too doesn't work out), but it is separating after underscore and the before underscore values I couldn't fetch.

In my case as I mentioned above in the convention I need to handle all the 3 scenarios of naming convention to separate the names without version.

Since I am blocked with this, please help will the inputs. Thanks !!

Kivi
  • 485
  • 1
  • 9
  • 26
  • Try `^(.*)_\d+\.\d+\.\d+\..*$` or match `_\d+\.\d+\.\d+\.v.*$` and replace with an empty string. – The fourth bird Sep 14 '18 at 08:15
  • try this one and take group 1, `(.*?)(_.*)` – The Scientific Method Sep 14 '18 at 08:26
  • Can there be underscores in this part `org.apache.commons.httpclient` before the first underscore? Or are they always characters separated by a dot? Would matching the first underscore until the rest of the string `_.*$` and then replace by an empty string work for your scenario? – The fourth bird Sep 14 '18 at 08:50
  • The above solutions "^(.*)_\d+\.\d+\.\d+\..*$" and "(.*?)(_.*)" worked well for me. Thanks a lot The fourth bird and The Scientific Method for the quick response. – Kivi Sep 14 '18 at 09:25

3 Answers3

0

Your regex _(.*) matches an underscore and captures in a capturing group any character zero or more times.

For your example data, if you want to match anything before the first underscore (so not taking any pattern before the underscore into account) then you could match the first underscore and then match any character zero or more times until the end of the string.

In the replacement use an empty string.

_.*$

Regex demo

<propertyregex property="MyRegex"
input="@{MyFolder}"
regexp="_.*$"
casesensitive="false"
replace=""/>

Or match from the start of the string ^ not an underscore one or more times using a negated character class [^_]+

^[^_]+

Regex demo

<propertyregex property="MyRegex"
input="@{MyFolder}"
regexp="^[^_]+"
select="\0"
casesensitive="false" />
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • what if `3.1.0.1.v20170718_1537` ? – The Scientific Method Sep 14 '18 at 08:32
  • My scenario is like, after underscore (org.apache.commons.httpclient"_"3.1.0.v20170718_1537), any value can be there, but that thing we don't need to consider. The only thing we need is the value before the 1st underscore. – Kivi Sep 14 '18 at 08:39
0

If you want the regex - please use this

(.*?)_(.*)

Demo here

But in your case the problem is the group you are selecting

   <propertyregex property="MyRegex"
                           input="@{MyFolder}"
                           regexp="(.*?)_(.*)"
                           select="\1"
                           casesensitive="false" />
0

If you don't want to fiddle around with regex you may use script task with builtin javascript engine, f.e.:

<project>
  <property name="foo" value="org.apache.commons.httpclient_3.1.0.v20170718_1537"/>

    <script language="javascript">
      project.setProperty('whatever', project.getProperty('foo').split('_')[0]);
    </script>

    <echo>$${whatever} => ${whatever}</echo>
</project>

If you use scripting regularly the groovy ant task is recommended - see Groovy scripts in Ant: Use script task or groovy task?

Rebse
  • 10,307
  • 2
  • 38
  • 66