Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6531821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:55:27+00:00 2026-05-25T09:55:27+00:00

How would you translate this Perl regex into Java? /pattern/i While compiles, it does

  • 0

How would you translate this Perl regex into Java?

/pattern/i

While compiles, it does not match “PattErn” for me, it fails

Pattern p = Pattern.compile("/pattern/i");
Matcher m = p.matcher("PattErn");

System.out.println(m.matches()); // prints "false"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:55:27+00:00Added an answer on May 25, 2026 at 9:55 am

    How would you translate this Perl regex into Java?

    /pattern/i
    

    You can’t.

    There are a lot of reasons for this. Here are a few:

    • Java doesn’t support as expressive a regex language as Perl does. It lacks grapheme support (like \X) and full property support (like \p{Sentence_Break=SContinue}), is missing Unicode named characters, doesn’t have a (?|...|...|) branch reset operator, doesn’t have named capture groups or a logical \x{...} escape before Java 7, has no recursive regexes, etc etc etc. I could write a book on what Java is missing here: Get used to going back to a very primitive and awkward to use regex engine compared with what you’re used to.

    • Another even worse problem is because you have lookalike faux amis like \w and and \b and \s, and even \p{alpha} and \p{lower}, which behave differently in Java compared with Perl; in some cases the Java versions are completely unusable and buggy. That’s because Perl follows UTS#18 but before Java 7, Java did not. You must add the UNICODE_CHARACTER_CLASSES flag from Java 7 to get these to stop being broken. If you can’t use Java 7, give up now, because Java had many many many other Unicode bugs before Java 7 and it just isn’t worth the pain of dealing with them.

    • Java handles linebreaks via ^ and $ and ., but Perl expects Unicode linebreaks to be \R. You should look at UNIX_LINES to understand what is going on there.

    • Java does not by default apply any Unicode casefolding whatsoever. Make sure to add the UNICODE_CASE flag to your compilation. Otherwise you won’t get things like the various Greek sigmas all matching one another.

    • Finally, it is different because at best Java only does simple casefolding, while Perl always does full casefolding. That means that you won’t get \xDF to match "SS" case insensitively in Java, and similar related issues.

    In summary, the closest you can get is to compile with the flags

     CASE_INSENSITIVE | UNICODE_CASE | UNICODE_CHARACTER_CLASSES
    

    which is equivalent to an embedded "(?iuU)" in the pattern string.

    And remember that match in Java doesn’t mean match, perversely enough.


    EDIT

    And here’s the rest of the story…

    While compiles, it does not match "PattErn" for me, it fails

       Pattern p = Pattern.compile("/pattern/i");
       Matcher m = p.matcher("PattErn");
       System.out.println(m.matches()); // prints "false"
    

    You shouldn’t have slashes around the pattern.

    The best you can do is to translate

    $line = "I have your PaTTerN right here";
    if ($line =~ /pattern/i) {
        print "matched.\n";
    }
    

    this way

    import java.util.regex.*;
    
    String line     = "I have your PaTTerN right here";
    String pattern  = "pattern";      
    Pattern regcomp = Pattern.compile(pattern, CASE_INSENSITIVE
                                            | UNICODE_CASE
                    // comment next line out for legacy Java \b\w\s breakage 
                                            | UNICODE_CHARACTER_CLASSES  
                                    );    
    Matcher regexec = regcomp.matcher(line);    
    if (regexec.find()) {
        System.out.println("matched");
    } 
    

    There, see how much easier that isn’t? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you translate this xcopy command into Robocopy: xcopy *.* C:\DestinationFolder\ Keeping in
How would you translate this into a Rails content_tag? <td rowspan=4>glah</td>
I've got some code which I would like to translate into Opengl ES. I'm
Would it not make sense to support a set of languages (Java, Python, Ruby,
I've read this article where the /^1?$|^(11+?)\1+$/ Perl regex is used to test if
I would like to translate a Perl package name to the full path of
I would like to translate this MySQL query to the Zend format: SELECT count_panne,
How do i do translate this linq code into sql? if (storeId > 1)
I want to use XMPP to push data to my client which would translate
I would like to translate or 301-redirect urls such as: www.domain.com/example.html to www.domain.com/example Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.