Regex-ANT: Find LAST occurence of char, and uppercase it.

N

natG

Hi;
I have followed the "greedy backwards" thread and it is over my head,
and I do not know if the solution there applies to this problem.

I'd like to find the *last* occurrence of a string (usually one
character) within a target (equivalent to string.lastindexof in Java),
and uppercase it. This, using Ant, hence I need a vanilla Regex solution.
Although at the very moment I need it to get the last dir of a path name
(and uppercase it), at a generic level, I find 'find backwards via
regex' extremely useful. So, if the solution can be a bit generic, it
would be great. But, again, at the moment, I want a regex to get 'dirc'
out of "dira/dirb/dirc" and uppercase it, so the final string is 'Dirc'.
(And although Ant has a 'basedir' tag, it [correctly ]does not uppercase
it.)

Tia
-nat
 
A

Alan Moore

Well, to find the last component in a path, you can use "[^/]+$", but
capitalizing it is a problem. In Perl, you would just stick "\u" on
the front of the replacement string, but no Java regex package that I
know of has that capability.
 
N

natG

Alan said:
Well, to find the last component in a path, you can use "[^/]+$", but
capitalizing it is a problem. In Perl, you would just stick "\u" on
the front of the replacement string, but no Java regex package that I
know of has that capability.
I wasn't aware that Regex engines differ that much in functionality.
Thanks
-nat
 
A

Alan Moore

Alan said:
Well, to find the last component in a path, you can use "[^/]+$", but
capitalizing it is a problem. In Perl, you would just stick "\u" on
the front of the replacement string, but no Java regex package that I
know of has that capability.
I wasn't aware that Regex engines differ that much in functionality.
Thanks

That's just it: \u is part of Perl's built-in string handling, and has
nothing to do with the regex engine. Here's a tool that lets you do
additional processing on the replacement string when you use the
jdk1.4 regex package:

http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html

It's a heckuva lot more verbose than \u, but at least it makes it
*possible* to do what you want.
 
N

natG

Alan said:
Alan Moore wrote:

Well, to find the last component in a path, you can use "[^/]+$", but
capitalizing it is a problem. In Perl, you would just stick "\u" on
the front of the replacement string, but no Java regex package that I
know of has that capability.

I wasn't aware that Regex engines differ that much in functionality.
Thanks


That's just it: \u is part of Perl's built-in string handling, and has
nothing to do with the regex engine. Here's a tool that lets you do
additional processing on the replacement string when you use the
jdk1.4 regex package:

http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html

It's a heckuva lot more verbose than \u, but at least it makes it
*possible* to do what you want.
But...I need to do this within an ANT script. If I need custom code, I'd
rather write an ant task.
Thanks;
-nat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top