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 799363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:06:50+00:00 2026-05-14T23:06:50+00:00

I have this situation(Java code): 1) a string such as : A wild adventure

  • 0

I have this situation(Java code):
1) a string such as : “A wild adventure” should match.
2) a string with adjacent repeated words: “A wild wild adventure” shouldn’t match.

With this regular expression: .* \b(\w+)\b\s*\1\b.* i can match strings containing adjacent repeated words.

How to reverse the situation i.e how to match strings which do not contain adjacent repeat words

  • 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-14T23:06:51+00:00Added an answer on May 14, 2026 at 11:06 pm

    Use negative lookahead assertion, (?!pattern).

        String[] tests = {
            "A wild adventure",      // true
            "A wild wild adventure"  // false
        };
        for (String test : tests) {
            System.out.println(test.matches("(?!.*\\b(\\w+)\\s\\1\\b).*"));
        }
    

    Explanation courtesy of Rick Measham’s explain.pl:

    REGEX: (?!.*\b(\w+)\s\1\b).*
    NODE                     EXPLANATION
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
        \b                       the boundary between a word char (\w)
                                 and something that is not a word char
    --------------------------------------------------------------------------------
        (                        group and capture to \1:
    --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1
                                   or more times (matching the most
                                   amount possible))
    --------------------------------------------------------------------------------
        )                        end of \1
    --------------------------------------------------------------------------------
        \s                       whitespace (\n, \r, \t, \f, and " ")
    --------------------------------------------------------------------------------
        \1                       what was matched by capture \1
    --------------------------------------------------------------------------------
        \b                       the boundary between a word char (\w)
                                 and something that is not a word char
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    --------------------------------------------------------------------------------
      .*                       any character except \n (0 or more times
                               (matching the most amount possible))
    

    See also

    • regular-expressions.info/Lookarounds

    Related questions

    • using regular expression in Java
      • Uses negative lookahead to ensure a string doesn’t have a character occuring more than once
    • Java split is eating my characters.
      • Many examples of using assertions
    • How do I convert CamelCase into human-readable names in Java?
      • Very instructive example of using lookarounds

    Note

    Negative assertions only make sense when there are also other patterns that you want to positively match (see examples above). Otherwise, you can just use boolean complement operator ! to negate matches with whatever pattern you were using before.

    String[] tests = {
        "A wild adventure",      // true
        "A wild wild adventure"  // false
    };
    for (String test : tests) {
        System.out.println(!test.matches(".*\\b(\\w+)\\s\\1\\b.*"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Rohan is right, inside Eclipse you open the DDMS perspective,… May 16, 2026 at 3:34 pm
  • Editorial Team
    Editorial Team added an answer http://www.actionscript.org/forums/showthread.php3?t=185370 AS3 Pass FlashVars to loaded swf if you have… May 16, 2026 at 3:34 pm
  • Editorial Team
    Editorial Team added an answer Set your start URL to be blank in the project… May 16, 2026 at 3:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In a certain Java class for a Struts2 web application, I have this line
This is the situation: We have some binary files (PDFs, PPTs, ZIPz, etc.) stored
Disclaimer: I have looked through this question and this question but they both got
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
I'm new to Java and I have come to having the following problem: I
I ran into a situation today where Java was not invoking the method I
I have found following question: $0 (Program Name) in Java? Discover main class? but
I'm gonna split this question into 'Situation' , 'Task' , 'Confusion' & 'Question' to
I have a simple RMI 'compute' server application (similar to this ) that accepts
What is really meant when using Java Date utilities and something has been deprecated.

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.