Converting string to uppercase using Perl5Substitution

  • Thread starter Sudeep Batra - Bond 007
  • Start date
S

Sudeep Batra - Bond 007

Hi,

We are using the following piece of code for Perl5Substitution.

public String substitute (String regExp, String sub, String input)
throws DatafileException {
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = null;
PatternCompiler compiler = new Perl5Compiler();
try {
pattern = compiler.compile(regExp);
} catch(MalformedPatternException e){
String errorMessage = "Bad pattern. " + e;
log.error(errorMessage);
throw new DatafileErrorException(errorMessage);
}
// Perform substitution
return Util.substitute(matcher, pattern, new
Perl5Substitution(sub, 0),input,99);
}

Lets say we want to convert a string such that every word should begin
with "A", then if we pass the following values as parameters to the
above, it works for us.

regExp = (\w+)
sub = A$1

This converts a string "Preferred Stock" to "APreferred AStock".

However, we are not able to find a way to convert the string to
uppercase. The following regular expression in perl script works fine
for us.
$variable=~ s/(\w+)/uc($1)/ge;

But we are not able to reproduce the same to work with
Perl5Substitution. \U doesnt seem to work even.

If anyone can suggest what we should pass as the regular expression
and the substitution string so that we get the desired, that would be
really appreciated.

Thanks
Sudeep
 
D

Dave Glasser

(e-mail address removed) (Sudeep Batra - Bond 007) wrote on 7 Apr 2004
06:29:03 -0700 in comp.lang.java.programmer:
Hi,

We are using the following piece of code for Perl5Substitution.

public String substitute (String regExp, String sub, String input)
throws DatafileException {
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = null;
PatternCompiler compiler = new Perl5Compiler();
try {
pattern = compiler.compile(regExp);
} catch(MalformedPatternException e){
String errorMessage = "Bad pattern. " + e;
log.error(errorMessage);
throw new DatafileErrorException(errorMessage);
}
// Perform substitution
return Util.substitute(matcher, pattern, new
Perl5Substitution(sub, 0),input,99);
}

Lets say we want to convert a string such that every word should begin
with "A", then if we pass the following values as parameters to the
above, it works for us.

regExp = (\w+)
sub = A$1

This converts a string "Preferred Stock" to "APreferred AStock".

However, we are not able to find a way to convert the string to
uppercase. The following regular expression in perl script works fine
for us.
$variable=~ s/(\w+)/uc($1)/ge;

But we are not able to reproduce the same to work with
Perl5Substitution. \U doesnt seem to work even.

If anyone can suggest what we should pass as the regular expression
and the substitution string so that we get the desired, that would be
really appreciated.

It might help if you posted the actual code you're using to create the
regular expression and substiution, so we can see the string literals
you're using. The problem might be due to some improper escaping.

Also, it looks like you can accomplish what you're trying to do with
String.toUpperCase(). Is there a reason you're not using that?
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top