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

  • SEARCH
  • Home
  • 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 9142629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:49:32+00:00 2026-06-17T09:49:32+00:00

I’m struggling to understand the behaviour of regular expressions in Java and have encountered

  • 0

I’m struggling to understand the behaviour of regular expressions in Java and have encountered something that seems very strange. In the code below, the test fails all of a sudden for reasons I don’t understand at the test with the message label “6 letters matched, negative” (the subsequent two tests fail as well). Have I been staring at this for too long or is there indeed something strange happening? I don’t this has to do with the variable-length negative lookahead assertion (?!X), but I’d be happy to hear any theories, or even a confirmation that others are experiencing the same issue and that it’s not specific to my JVM. Sorry that the regular expression is so contrived, but you don’t want to see the real thing 🙂

// $ java -version
// java version "1.7.0_10"
// Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
// Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

// test of word without agreement
String test = "plusieurs personne sont";

// match the pattern with curly braces
assertTrue("no letters matched", Pattern.compile("plusieurs personne\\b").matcher(test).find());
assertTrue("1 letters matched", Pattern.compile("plusieurs personn\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("2 letters matched", Pattern.compile("plusieurs person\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("3 letters matched", Pattern.compile("plusieurs perso\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("4 letters matched", Pattern.compile("plusieurs pers\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("5 letters matched", Pattern.compile("plusieurs per\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("6 letters matched", Pattern.compile("plusieurs pe\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("7 letters matched", Pattern.compile("plusieurs p\\p{Alpha}{1,100}\\b").matcher(test).find());
assertTrue("8 letters matched", Pattern.compile("plusieurs \\p{Alpha}{1,100}\\b").matcher(test).find());

// match the negative pattern (without s or x) with curly braces
assertTrue("no letters matched, negative", Pattern.compile("plusieurs (?!personne[sx])\\w+").matcher(test).find());
assertTrue("1 letters matched, negative", Pattern.compile("plusieurs (?!personn\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("2 letters matched, negative", Pattern.compile("plusieurs (?!person\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("3 letters matched, negative", Pattern.compile("plusieurs (?!perso\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("4 letters matched, negative", Pattern.compile("plusieurs (?!pers\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("5 letters matched, negative", Pattern.compile("plusieurs (?!per\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
// the assertion below fails (is false) for reasons unknown
assertTrue("6 letters matched, negative", Pattern.compile("plusieurs (?!pe\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("7 letters matched, negative", Pattern.compile("plusieurs (?!p\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
assertTrue("8 letters matched, negative", Pattern.compile("plusieurs (?!\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find());
  • 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-06-17T09:49:33+00:00Added an answer on June 17, 2026 at 9:49 am

    Let’s see how the lookahead matches:

    pe           literal, matches "pe"
    r            matches \p{Alpha}{1,100}
    s            matches [sx]
    

    So negative lookahead doesn’t match (the tail of your string, "onne sont", is irrelevant here).

    Placing \\b after [sx] may help, if your idea is that the next word should not end with s or x. Always have in mind that the negative lookahead won’t be sorry about failing, and it won’t backtrack in order to find how it’s possible to make your regexp not match.

    UPD: Let’s look closer at case 5 to compare it with case 6. Here we work with hypothetical match (for the expression inside lookahead), so we have to consider several variants of how it could (almost) happen.

    per          literal, would match "per" -- it's always so
                 -- let's try to imagine how the rest could match:
    sonn         would match \p{Alpha}{1,100}
    e            wouldn't match [sx], FAIL
                 -- or maybe
    s            would match \p{Alpha}{1,100}
    o            wouldn't match [sx], FAIL
                 -- or maybe yet
    so           would match \p{Alpha}{1,100}
    n            wouldn't match [sx], FAIL.
    

    We would have another interesting adventure if the second word was “personalisation”.

    UPD2: The discussion in comment prompted me to add a generalization here:

    Regular expressions are attractive because they share an important trait of human mind: confirmation bias. When we write regular expressions, we want them to match; even if our job is to prevent invalid input, we think about valid inputs most of the time. Regular expression matcher normally shares this property: it wants to match and hates to fail. That’s why a subexpression like \p{Alpha}{1,100} doesn’t mean “eat the longest chunk of Alphas available, before trying to match the rest of input”. It means, roughly, “consider every possible chunk of Alphas with length within [1,100], finding a way for the whole expression to match”.

    That’s why with regexps, it’s easy to overlook false positives of complex expressions: invalid inputs that are erroneously accepted. This problem doesn’t appear, it just becomes more visible when we use negative lookaheads:

    Inside a negative lookahead, regexp matcher wants to match the inner expression (making outer expression fail). Human programmer still wants to match the outer expression; as we see in our example, this factor does affect our reasoning about the inner expression. We think that it shouldn’t try so hard to match (and, e.g., that it should handle subexpressions in a dumb way, instantly eating the longest input possible). The matcher works as usually, but our ideas on desirable behavior are now out of sync with its algorithm. And false positives for the inner expression (which are hard to notice), become false negatives for the outer one (which we notice and hate).

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.