regular expression in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ye Zhou

    regular expression in Java

    Hi all,

    I am trying to convert a perl program into java and I got stuck in the
    regular expression part. It seems like java.util.regex couldn't help
    much. I checked with Apache regexp and didn't get the right way to do
    it either. Therefore, I am writing to see if someone could throw a
    light on how to do a translation on the statement below.

    if ($xml =~ m/^.*?<Transactio n.*?instanceId= "(.*?)".*?recei ver="(.*?)".*?s ender="(.*?)".* ?transactionId= "(.*?)".*?/s)
    {
    return ($1, $4, $3, $2);

    Your help is greatly appreciated.

    Thanks,
    Ye
  • Sideshow Alex

    #2
    Re: regular expression in Java

    [email protected] (Ye Zhou) wrote in
    news:c4d0da26.0 307301650.6ec8a [email protected] gle.com:
    [color=blue]
    > Hi all,
    >
    > I am trying to convert a perl program into java and I got stuck in the
    > regular expression part. It seems like java.util.regex couldn't help
    > much. I checked with Apache regexp and didn't get the right way to do
    > it either. Therefore, I am writing to see if someone could throw a
    > light on how to do a translation on the statement below.
    >
    > if ($xml =~
    > m/^.*?<Transactio n.*?instanceId= "(.*?)".*?recei ver="(.*?)".*?[/color]
    sender="(.[color=blue]
    > *?)".*?transact ionId="(.*?)".* ?/s) {
    > return ($1, $4, $3, $2);
    >
    > Your help is greatly appreciated.
    >
    > Thanks,
    > Ye[/color]


    Try this...

    String regex = ".*?<Transactio n.*?instanceId= "(.*?)".*?recei ver="(.*?)".
    *?sender="(.> *?)".*?transact ionId="(.*?)".* ?";

    Pattern p = Pattern.compile (regex);
    Matcher m = pattern.match(x ml);
    if (m.matches())
    return new String[] {m.group(1), m.group(4), m.group(3), m.group(2)};


    Cheers,
    Alex

    Comment

    Working...