perl regex to java regex

Discussion in 'Java' started by Rick Venter, Oct 29, 2003.

  1. Rick Venter

    Rick Venter Guest

    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, Oct 29, 2003
    #1
    1. Advertising

  2. Rick Venter

    VisionSet Guest

    "Rick Venter" <> wrote in message
    news:...
    > 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, Oct 29, 2003
    #2
    1. Advertising

  3. "VisionSet" <> schrieb im Newsbeitrag
    news:W7Tnb.3734$...
    > "Rick Venter" <> wrote in message
    > news:...
    > > 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
     
    Robert Klemme, Oct 30, 2003
    #3
  4. Rick Venter

    Dave Monroe Guest

    (Rick Venter) wrote in message news:<>...
    > 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
     
    Dave Monroe, Oct 30, 2003
    #4
  5. In article <>,
    (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/
     
    Mike Baranczak, Nov 5, 2003
    #5
  6. Rick Venter

    Ant... Guest

    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]*"
     
    Ant..., Nov 6, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,839
  2. Replies:
    2
    Views:
    619
  3. Replies:
    3
    Views:
    808
    Reedick, Andrew
    Jul 1, 2008
  4. /usr/ceo
    Replies:
    32
    Views:
    1,948
    Mark Space
    Sep 15, 2008
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    323
    PerlFAQ Server
    Jan 23, 2011
Loading...

Share This Page