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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:11:23+00:00 2026-05-25T14:11:23+00:00

I have a string of text like this: This is a[WAIT] test. What I

  • 0

I have a string of text like this:

This is a[WAIT] test.

What I want to do is search the string for a substring that starts with [ and ends with ]
Each one I find I want to add it to an ArrayList and replace substring in original string with a ^

Here is my regex:

String regex_script = "/^\\[\\]$/"; //Match a string which starts with the character [ ending in the character ] 

Here is what I have so far:

StringBuffer sb = new StringBuffer();

Pattern p = Pattern.compile(regex_script); // Create a pattern to match
Matcher m = p.matcher(line);  // Create a matcher with an input string
boolean result = m.find();
        while(result) {
                m.appendReplacement(sb, "^");
                result = m.find();
        }
        m.appendTail(sb); // Add the last segment of input to the new String

how would I got about doing this? Thanks

  • 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-25T14:11:24+00:00Added an answer on May 25, 2026 at 2:11 pm

    you can do:

        String regex_script = "\\[([^\\]]*)\\]";
    
        String line = "This is a[WAIT] testThis is a[WAIT] test";
        StringBuffer sb = new StringBuffer();
        List<String> list = new ArrayList<String>();   //use to record
    
        Pattern p = Pattern.compile(regex_script); // Create a pattern to match
        Matcher m = p.matcher(line); // Create a matcher with an input string
    
        while (m.find()) {
            list.add(m.group(1));
            m.appendReplacement(sb, "[^]");
        }
        m.appendTail(sb); // Add the last segment of input to the new String
    
        System.out.println(sb.toString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that looks like this: public static String escape(String text) {
i have a string like this one: $string = some text http://dvz.local/index/index/regionId/28 http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
I have a huge string that looks like this: Text Text Text Text Text
I have got a text string like this: test1test I want to check if
I have a problem that looks like this: My string of text looks like
I have string like this <root><text>My test is > & < </text></root> Actually this
I have a string that looks like this: TEXT MORETEXT 227.905 174.994 180 1111
I have a string like this; string text = 6A7FEBFCCC51268FBFF; And I have one
I have a string like this String text = <p><span><span id=test>Meanwhile, the Cougars are
in vb.net i have a string that looks like this text text text so

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.