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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:58:14+00:00 2026-06-15T00:58:14+00:00

Is there a way to get only the unique matches? without using a list

  • 0

Is there a way to get only the unique matches? without using a list or a map after the matching, I want the matcher output to be unique right away.

Sample input/output:

String input = "This is a question from [userName] about finding unique regex matches for [inputString] without using any lists or maps. -[userName].";
Pattern pattern = Pattern.compile("\\[[^\\[\\]]*\\]");
Matcher matcher = pattern.matcher(rawText);
while (matcher.find()) {
    String tokenName = matcher.group(0);
    System.out.println(tokenName);
}

This will output the following:

[userName]
[inputString]
[userName]

But I want it to output the following:

[userName]
[inputString]
  • 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-15T00:58:16+00:00Added an answer on June 15, 2026 at 12:58 am

    Yes there is. You can combine a negative lookahead and a backreference:

    "(\\[[^\\[\\]]*\\])(?!.*\\1)"
    

    That will only match if that, which was matched by your actual pattern, does not occur again in the string. Effectively, that means you always get the last occurrence of every match, so you would get them in a different order:

    [inputString]
    [userName]
    

    If the order is a problem for you (i.e. if it’s crucial to order them by first occurrence), you won’t be able to do this using regex only. You would need a variable-length look*behind* for that, and that is not supported by Java.

    Further reading:

    • Lookarounds
    • Backreferences

    Some notes on a general solution

    Note that this will work with any pattern whose matches are of non-zero width. The general solution is simply:

    (yourPatternHere)(?!.*\1)
    

    (I left out the double backslash, because that only applies to a few languages.)

    If you want it to work with patterns that have zero-width matches (because you only want to know a position and are using lookarounds only for some reason), you could do this:

    (zeroWidthPatternHere)(?!.+\1)
    

    Also, note that (generally) you might have to use the “singleline” or “dotall” option, if your input may contain linebreaks (otherwise the lookahead will only check in the current line). If you cannot or do not want to activate that (because you have a pattern that includes periods which should not match line breaks; or because you use JavaScript), this is the general solution:

    (yourPatternHere)(?![\s\S]*\1)
    

    And to make this answer even more widely applicable, here is how you could match only the first occurrence of every match (in an engine with variable-length lookbehinds, like .NET):

    (yourPatternHere)(?<!\1.*\1)
    or
    (yourPatternHere)(?<!\1[\s\S]*\1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to get only the unique values out of a
is there any way in Oracle, to get only the dd-mm-yyyy part from an
Is there a way to get some text to appear only when the [draft]
Is there any way to get the previous hash using the history object ?I
Is there a way to get HTML contents from TinyMCE editor using jQuery so
I want to add only unique records into a database table. Is there any
http://api.jquery.com/jQuery.unique/ lets you get only unique elements. Is there I can find out if
Is there way to get file from windows xp command prompt? I tried to
is there way how to get name ov event from Lambda expression like with
Is there a way to get the item pointed at by an iterator in

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.