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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:53:14+00:00 2026-06-07T20:53:14+00:00

I am trying to create a regex in Java to match the pattern of

  • 0

I am trying to create a regex in Java to match the pattern of a particular word to find other words with the same pattern. For example, the word “tooth” has the pattern 12213 since both the ‘t’ and ‘o’ repeat. I would want the regex to match other words like “teeth”.

So here’s my attempt using backreferences. In this particular example, it should fail if the second letter is the same as the first letter. Also, the last letter should be different from all the rest.

String regex = "([a-z])([a-z&&[^\1]])\\2\\1([a-z&&[^\1\2]])";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher("tooth");

//This works as expected
assertTrue(m.matches());

m.reset("tooto");
//This should return false, but instead returns true
assertFalse(m.matches());

I have verified that it works on examples like “toot” if I remove the last group, i.e. the following, so I know the backreferences are working up to this point:

String regex = ([a-z])([a-z&&[^\1]])\\2\\1";

But if I add back the last group to the end of the pattern, it’s like it doesn’t recognize the backreferences inside the square brackets anymore.

Am I doing something wrong, or is this a bug?

  • 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-07T20:53:16+00:00Added an answer on June 7, 2026 at 8:53 pm

    If you print your regex you get a clue what is wrong, the backreferences in your groups are actually escaped by Java to produce some weird characters. Therefore it doesn’t work as expected. For example:

    m.reset("oooto");
    System.out.println(m.matches());
    

    also prints

    true

    Also, && doesn’t work in regexes, you will have to use lookahead instead. This expression works for your example above:

    String regex = "([a-z])(?!\\1)([a-z])\\2\\1(?!(\\1|\\2))[a-z]";
    

    The expression (?!\\1) looks ahead to see that the next charachter isn’t the first one in the expression, without moving the regex cursor forward.

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

Sidebar

Related Questions

I'm trying to create a regex expression to match any string that has exactly
I'm trying to create a regex that will match only when the string has
I'm trying to create a regex pattern to match the lines in the following
I am trying to create a regex that does not match a word (a-z
I am trying to create a suitable regular expression to use java.util.regex.Pattern I am
I'm trying to create a regex to tokenize a string. An example of the
Trying to create a regex pattern for email address check. That will allow a
I am trying to create a regex that will match the first 3 characters
I'm trying to create a regex pattern to extract A tags in html that
I'm trying to create a regex pattern that allows the user to create a

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.