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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:15:06+00:00 2026-05-26T05:15:06+00:00

Given a string containing some number of square brackets and other characters, I want

  • 0

Given a string containing some number of square brackets and other characters, I want to find all closing square brackets preceded by an opening square bracket and some number of letters.
For instance, if the string is

] [abc] [123] abc]

I want to find only the second closing bracket.

The following regex

(?<=[a-z]+)\]

will find me the second closing bracket, but also the last one:

] [abc] [123] abc]

Since I want to find only the first one, I make the obvious change to the regex…

(?<=\[[a-z]+)\]

…and I get “Look-behind group does not have an obvious maximum length near index 11.”

\[ is only a single character, so it seems like the obvious maximum length should be 1 + whatever the obvious maximum length was of the look-behind group in the first expression. What gives?


ETA: It’s not specific to the opening bracket.

(?<=a[b-z]+)\]

gives me the same error. (Well, at index 12.)

  • 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-26T05:15:06+00:00Added an answer on May 26, 2026 at 5:15 am

    \[ is only a single character, so it seems like the obvious maximum length should be 1 + whatever the obvious maximum length was of the look-behind group in the first expression. What gives?

    That’s the point, “whatever the obvious maximum length was of the look-behind group in the first expression”, is not obvious. A rule of fist is that you can’t use + or * inside a look-behind. This is not only so for Java’s regex engine, but for many more PCRE-flavored engines (even Perl’s (v5.10) engine!).

    You can do this with look-aheads however:

    Pattern p = Pattern.compile("(?=(\\[[a-z]+]))");
    Matcher m = p.matcher("] [abc] [123] abc]");
    while(m.find()) {
      System.out.println("Found a ']' before index: " + m.end(1));
    }
    

    (I.e. a capture group inside a look ahead (!) which can be used to get the end(...) of the group)

    will print:

    Found a ']' before index: 7

    EDIT

    And if you’re interested in replacing such ]‘s, you could do something like this:

    String s = "] [abc] [123] abc] [foo] bar]";
    System.out.println(s);
    System.out.println(s.replaceAll("(\\[[a-z]+)]", "$1_"));
    

    which will print:

    ] [abc] [123] abc] [foo] bar]
    ] [abc_ [123] abc] [foo_ bar]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a text string containing a type name, is there some way to get
I have some rather long string containing just about anything that I want to
How can I drop all tables whose names begin with a given string? I
I have a requirement to disallow backslash characters in a given string field defined
Algorithm to generate all possible letter combinations of given string down to 2 letters
I have designed a class containing some information about a given object which will
I've got a map containing some keys (Strings) and values (POJOs) I want to
Given a 1GB(very large) file containing words (some repeated), we need to read the
I have an array containing words, some of them with accents. I want to
I'm trying to compress any given string to a shorter version, copy paste-able compressed

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.