RE: Newbee needs help

"Jones, David G" <[email protected]> Mon, 22 Jul 2002 16:34:29 +0100
Newsgroups gmane.comp.jakarta.oro.user
Message-ID <5158BC6D771ED3118D6200508B2C2722395C10@gbrxmsms01.bh.gbr.xerox.com>
I think you'll need to use MatchResult's method group(int) which gets the
contents of a parenthesised group ($1, $2 etc. in Perl).
If you want to do substitutions take a look at Perl5Substitution.

For example (pardon any typos!):

String aRegex = "^(\d+)-(\d+)";
PatternCompiler compiler = new Perl5Compiler();
PatternMatcher matcher = new Perl5Matcher();

try{
	Pattern compiledPattern = compiler.compile(aRegex);
	if(matcher.matches(inputString, compiledPattern)){
      	MatchResult mResult = matcher.getMatch();
            String prefix = mResult.group(1);
            String p1 = mResult.group(2);
		...
      }
} catch(MalformedPatternException mpe){
              ...
}

hth, my Perl's pretty poor so I hope I got the right idea of what you wanted
to do.

Dave

-----Original Message-----
From: Rupinder Singh Mazara [mailto:[email protected]]
Sent: 22 July 2002 14:46
To: [email protected]
Subject: Newbee needs help



Hi all

 I am a new user to ORO and need some help
I would like to  execute the following perl code in java

$pages = "something" ;
if ($pages =~ /^(\d+)-(\d+)/){
    ($p1,$p2) = ($1,$2);
} elsif ($pages =~ /^(\d+)$/){
    ($p1) = $1;
  } elsif ($pages =~ /^([A-Za-z]+)(\d+)$/){
    ($prefix,$p1) = ($1,$2);
  } elsif ($pages =~ /^([A-Za-z]+)(\d+)-(\d+)$/){
    ($prefix,$p1,$p2) = ($1,$2,$3);
  } elsif ($pages =~ /^([A-Za-z]+)(\d+)-(\1)(\d+)$/){
    ($prefix,$p1,$p2) = ($1,$2,$4);
  } elsif ($pages =~ /^(\d+)([A-Za-z]+)-(\d+)(\2)$/){
    ($p1,$suffix,$p2) = ($1,$2,$3);
  } elsif ($pages =~ /^(\d+)([A-Za-z]+)$/){
    ($p1,$suffix) = ($1,$2);
  } else {
    warn "Cant parse pages '$pages'";
  }


What would be the best way to solve this ?  :-(
I was considering using Perl4Util ... but donot know how to access
($1,$2)
Rupinder