f



perl regex to java regex

I am trying to convert a piece of code from using perl regexes to
using java regexes.

I need some help with converting the following two PERL syntax regex's
to java syntax regex's. Can anyone please help

1. FIRST_PERL_REGEX = "/^[\\d]*$/"

2. SECOND_PERL_REGEX = "/^[\\d]*/"

(All I am trying to do in the above regex's is try to look for an
integer)

essentially I need the equivalent FIRST_JAVA_REGEX and
SECOND_JAVA_REGEX for the above.
0
10/29/2003 5:01:53 PM
comp.lang.java.programmer 52714 articles. 0 followers. Post Follow

5 Replies
737 Views

Similar Articles

[PageSpeed] 21

"Rick Venter" <rick_venter@yahoo.com> wrote in message
news:e6f6eb95.0310290901.38be6c02@posting.google.com...
> I am trying to convert a piece of code from using perl regexes to
> using java regexes.
>
> I need some help with converting the following two PERL syntax regex's
> to java syntax regex's. Can anyone please help
>
> 1. FIRST_PERL_REGEX = "/^[\\d]*$/"
>
> 2. SECOND_PERL_REGEX = "/^[\\d]*/"
>
> (All I am trying to do in the above regex's is try to look for an
> integer)
>

"^\\d+$"

-- 
Mike W


0
spam19 (1055)
10/29/2003 5:56:34 PM
"VisionSet" <spam@ntlworld.com> schrieb im Newsbeitrag
news:W7Tnb.3734$Zr6.3283@newsfep4-winn.server.ntli.net...
> "Rick Venter" <rick_venter@yahoo.com> wrote in message
> news:e6f6eb95.0310290901.38be6c02@posting.google.com...
> > I am trying to convert a piece of code from using perl regexes to
> > using java regexes.
> >
> > I need some help with converting the following two PERL syntax regex's
> > to java syntax regex's. Can anyone please help
> >
> > 1. FIRST_PERL_REGEX = "/^[\\d]*$/"

final static Pattern FIRST_PERL_REGEX = Pattern.compile( "^\\d*$" );

> >
> > 2. SECOND_PERL_REGEX = "/^[\\d]*/"

final static Pattern SECOND_PERL_REGEX = Pattern.compile( "^\\d*" );

But note: when using Matcher.match() in Java it does an implicit anchor,
so you don't need "^" and "$" in the first expression while you should
append ".*" to the second.

> > (All I am trying to do in the above regex's is try to look for an
> > integer)
> >
>
> "^\\d+$"

"+" instead of "*"?  Not a faithful translation IMO, although a reasonable
one.  Typically I use this for matching integers:

/[+-]?\d+/

Pattern.compile( "[+-]?\\d+" )

Regards

    robert

0
bob.news (3807)
10/30/2003 10:56:03 AM
rick_venter@yahoo.com (Rick Venter) wrote in message news:<e6f6eb95.0310290901.38be6c02@posting.google.com>...
> I am trying to convert a piece of code from using perl regexes to
> using java regexes.
> 
> I need some help with converting the following two PERL syntax regex's
> to java syntax regex's. Can anyone please help
> 
> 1. FIRST_PERL_REGEX = "/^[\\d]*$/"
> 
> 2. SECOND_PERL_REGEX = "/^[\\d]*/"
> 
> (All I am trying to do in the above regex's is try to look for an
> integer)
> 
> essentially I need the equivalent FIRST_JAVA_REGEX and
> SECOND_JAVA_REGEX for the above.

How about:

    "^[0-9]*$" // matches numeric string (0 or more occurrances)

This avoids the pre-defined patterns that are peculiar to a given language.

Dave Monroe
0
monroeds (81)
10/30/2003 1:22:59 PM
In article <e6f6eb95.0310290901.38be6c02@posting.google.com>,
 rick_venter@yahoo.com (Rick Venter) wrote:

> I am trying to convert a piece of code from using perl regexes to
> using java regexes.
> 
> I need some help with converting the following two PERL syntax regex's
> to java syntax regex's. Can anyone please help
> 
> 1. FIRST_PERL_REGEX = "/^[\\d]*$/"
> 
> 2. SECOND_PERL_REGEX = "/^[\\d]*/"
> 
> (All I am trying to do in the above regex's is try to look for an
> integer)
> 
> essentially I need the equivalent FIRST_JAVA_REGEX and
> SECOND_JAVA_REGEX for the above.

Have you looked into the GNU java-regexp package? If memory serves me, 
it uses PERL syntax by default. Another bonus: it doesn't require Java 
1.4.

-MB

-- 
http://macbuild.sourceforge.net/
0
Mike
11/5/2003 5:03:42 PM
Mike Baranczak wrote:
....
> 
> Have you looked into the GNU java-regexp package? If memory serves me, 
> it uses PERL syntax by default. Another bonus: it doesn't require Java 
> 1.4.

Java 1.4's syntax is essentially Perl Syntax, the only difference
being the addition of possessive quantifiers, and the lack of support
for comments and certain constructs such as conditionals and
preprocessing.

Hence:

1. FIRST_JAVA_REGEX = "^[\\d]*$"

2. SECOND_JAVA_REGEX = "^[\\d]*"
0
news288 (16)
11/6/2003 10:55:28 AM
Reply: