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