perl regex to java regex

R

Rick Venter

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.
 
V

VisionSet

Rick Venter said:
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+$"
 
R

Robert Klemme

VisionSet said:
Rick Venter said:
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.

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

/[+-]?\d+/

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

Regards

robert
 
D

Dave Monroe

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
 
M

Mike Baranczak

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
 
A

Ant...

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]*"
 

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,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top