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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:09:57+00:00 2026-05-22T21:09:57+00:00

Given the following example string: [ One].[Two ].[ Three ].[Four] I want to match

  • 0

Given the following example string: "[ One].[Two ].[ Three ].[Four]"
I want to match “One”; “Two”, “Three” and “Four”.

In other words: I need to get the word between the brackets, regardless how many white spaces are surround this word.

I’ve tried it with the following expression:

(?<=\[)(?s)(.*?)(?=\s*\])

That results in " One", "Two", " Three" and "Four".

EDIT:
It’s a little bit more complicated than I first tought it would be:

  1. There are many (at least one) word(s) encapsulated by brackets which might seperated by an arbitrary char (e.g. "[one]" or "[one] [two][three].[four]").
  2. The brackets contain one single word and many, or even no whitespaces (e.g. "[one]" or "[two ]" or "[ three ]".
  3. These blocks of words and there enclosing brackets are surrounded by a known sequence of chars:
    "These words [word-1] .. [word-n] are well known" or
    "These words [word-1] .. [word-n] are well known".

Please note that "[word-1] .. [word-n]" just stands for an arbitrary count of the blocks described above.

I want to match just the single word(s) between the brackets and eliminate the surround sequence ("These words" and "are well known") as well as possibly existing whitespaces within the brackets and between the blocks. In addition, the possibly existing char (it couldn’t be more than only one) between the blocks should be eliminiated, too.
Hope that wasn’t too weird 😉

  • 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-22T21:09:58+00:00Added an answer on May 22, 2026 at 9:09 pm

    You can use this, with the “global” flag enabled

    \[\s*(\S+?)\s*\]
    

    Explanation

    \[      # a literal "["
    \s*     # any number of white space
    (\S+?)  # at least one non white-space character, non-greedily (group 1)
    \s*     # any number of white space
    \]      # a literal "]"
    

    EDIT:

    @Kobi noted that \S+? can actually match the ] in targets like "[ One]". So for a moment, group 1 would contain "One]".

    But then there still is the \] at the end of the regex, at which point the regex engine would backtrack and give the "]" to \], so the expression can succeed.

    It is vitally important to use on-greedy matching here (\S+?, as opposed to \S+). I got that wrong in the first version of my answer as well.

    Further, the \S is very unspecific. If you have anything more specific in terms of what “a word” is for you – by all means, use it.

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

Sidebar

Related Questions

Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
How can you sort the following word by the given rule? Example data is
I need to provide following functionality for one of the web sites. http://www.example.com/ [sponsor]
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given a sorted vector with a number of values, as in the following example:
This is just an example, but given the following model: class Foo(models.model): bar =
Given a group of files with the following naming convention: datetime_restofname.txt an example of

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.