Extract string between two '_' - regex

I'm looking for an ANT regex to extract string between two _ or between abc_ and _
Below is the regex I'm trying, with no luck.
/xxx/xx_x/xxxx/abc_stringtoextract_x.x.zip
I need to fetch stringtoextract,
<propertyregex property="name"
input="#{process}"
regexp="(?<=abc_)(.*?)(?=\_)"
select="\1"
casesensitive="false"/>
<echo>#{process}</echo>

Replace < with the character entity <. Also the result is saved in the name property specified in the property attribute, and not the process parameter, so you should echo the right result.
<propertyregex property="name"
input="${process}"
regexp="(?<=abc_)(.*?)(?=\_)"
select="\1"
casesensitive="false"/>
<echo>${name}</echo> <!-- instead of process -->

.*abc_([^_]*)
Fixed the issue.

Related

How to get the index by regular expression in ANT

I have a string with a version as .v_september (every month it will vary). In this i wanted to take the value after underscore, which means "sep" (First 3 letters alone).
By using the regex .v_(.*) i am able to take the complete month and not able to get the first 3 letters alone.
Can someone help me out how can I achieve this in Apache ANT.
Thanks !
Regex functions on properties are a bit awkward in native Ant (as opposed to working with text within files). Ant-contrib has the replaceregexp task, but I try to avoid ant-contrib whenever possible.
Instead, it can be accomplished with the loadfile task and a nested filter:
<property name="version" value=".v_september" />
<loadfile property="version.month.short">
<propertyresource name="version" />
<filterchain>
<tokenfilter>
<replaceregex pattern="\.v_(.{3}).*" replace="\1" />
</tokenfilter>
</filterchain>
</loadfile>
<echo message="${version.month.short}" />
Regarding the regex pattern, note how it needs to end with .*. This is because Ant doesn't have a "match" function that simply returns the content of a capture group. It's just running a replacement, so we need to replace everything in the string that isn't part of the group.
.* will capture everything and for limiting to capturing only three characters you need to write {3} instead of *. Also you should escape the . in the beginning of your regex to only match a literal dot. You can use this regex and capture from group1,
\.v_(.{3})
Demo

Regex search in XSL, select string after match

I have a solution where the filename has a prefix showing the filesize of a PDF. I need to pick up that value in to a XML-file that has a lot of other info that is collected with the XSLT.
How ever I can't get just this Regex match to work.
Filename have this structure as this example:
776524_P9466_Novilon_Broschyr_SE_Omslag.xml where the digits before the underscore is the filesize.
I have a Regex search pattern of _(.*) and I can validate that it will match everything after the first section of the digits.
Here is my XSL that I'm having problems with:
<xsl:param name="find_size">
<xsl:text>(_.*)</xsl:text>
</xsl:param>
<xsl:variable name="filename_of_start"><xsl:value-of select="replace($filename_of_file, '$find_size', '')"/></xsl:variable>
<artwork_size><xsl:value-of select="$filename_of_start"/></artwork_size>
$filename_of_file has the string: 776524_P9466_Novilon_Broschyr_SE_Omslag.xml
I have also tried to match the digits before the underscore and replace with that match but haven't got that to work either. Other replaces where I remove other matches from the beginning of the string works.
Thanks
How about using the substring-before() XPath function?
<xsl:variable name="file_size" select="substring-before($filename, '_')" />
Instead of replace($filename_of_file, '$find_size', '') you want replace($filename_of_file, $find_size, '').

regex pattern for replacement of string

I am trying to replace the string with the below regex pattern but it is not getting replaced. I tried different combinations also but nothing worked. Any Idea?
<regex pattern="jre64\/1\.6\.0"
replacement="jre64/1.7.0" />

ant propertyregex, replace string with specified string

this is my string com.element.subelement
I want to use propertyregex to replace all . with /, and use that output as a directory path.
It seemed like propertyregex would be the tool for this, but maybe I am using its regexp property incorrectly.
How would I use regex to find the . only, I don't want to assume that the string is 3 alphabet portions separated by periods. also perhaps propertyregex is not the tool for the job
here is one that works where I assume that a package name only have 2 periods
<!-- set package name to folder directory -->
<propertyregex
property="current.target.dir"
input="com.element.subelement"
regexp="(.+?)\.(.+?)\.(.+?)"
replace="\1/\2/\3"
casesensitive="false" />
<echo>${current.target.dir}</echo>
Simply replacing \. with / should accomplish what you're looking for, if I understand. However, this will only replace the first instance on a line by default, so you have to add the property global="true":
<propertyregex
property="current.target.dir"
input="com.element.subelement"
regexp="\."
replace="/"
global="true" />

Using ant <propertyregex>, how can I capture the /etc/shadow record for a user?

From ant, we want to extract a line from an old /etc/shadow file, capturing the line for a specific user name, such as "manager". This is part of a backup/restore operation. What we used previously was not specific enough, so it would match users like "mymanager", so we tried to tighten it down by anchoring the start of the string to beginning of the line (typically "^"). This definitely did not work as we expected.
How can we anchor so that we get an exact match for a username? -- answered below.
First attempt, which gave the wrong result if we had a user of "mymanager" in the /etc/shadow file copy:
<loadfile property="oldPasswords" srcFile="${backup.dir}/shadow"/>
<propertyregex property="manager.backup" input="${oldPasswords}"
regexp="(manager\:.*)" select="\1" casesensitive="true" />
Second attempt, which failed because "^" is not interpreted in the normal regular expression way by default:
<loadfile property="oldPasswords" srcFile="${backup.dir}/shadow"/>
<propertyregex property="manager.backup" input="${oldPasswords}"
regexp="^(manager\:.*)" select="\1" casesensitive="true" />
Kobi suggested adding -> flags="m" <- which sounded good but ant reported that the flags option is not supported by propertyregex.
The final, successful, approach required inserting "(?m)" at the beginning of the regexp: That was the essential change.
<propertyregex property="manager.backup" input="${oldPasswords}"
regexp="(?m)^manager:.*$" select="\0" casesensitive="true" />
The regexp with propertyregex appears to follow the rules in this documentation of regular expressions in Java (search for "multiline" for example): http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
Check the above document if you have similar questions about how to make propertyregex and regexp do what you want them to do!
THANKS! Solved.
Alan Carwile
I think the m(ultiline) flag is what you want to use and will give the start-of-line anchor the right behavior. It's possible to change flags within the regular expression with the syntax (?<flagstoturnon>-<flagstoturnoff>). So in your case, adding (?m) to the start of the regular expression (before the caret) should work.