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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:29:11+00:00 2026-05-24T03:29:11+00:00

Ok, you might say that this is a duplicate post but it is different.

  • 0

Ok, you might say that this is a duplicate post but it is different.

I am working on a program that is working on some kind of deleting delimiters specified by the user. My program is working if the delimiter is only a single character (special or not). However, if the user input is a string, it removes the all characters of the delimiter from the message string.

ex. String message = “ab\nc[d]e{fMardk1g(h)i}j”;
output will be : bcefghij
but the expected output is abcdefghij

I’m new in using the Pattern class, so I don’t know where the problem lies.

Here’s the code in question (I put it in a testing class so I can isolate the problem):

import java.util.regex.Pattern;

public class ParsingTest {
    public static void main(String[] args) {
        String[] delimiters = { "Mardk1", "\n", "[", "]", "{", "}", "(", ")" };  
        StringBuilder regexp = new StringBuilder("");  
        regexp.append("[");  
        for(String s : delimiters) {  
            regexp.append("[");  
            regexp.append(Pattern.quote(s));  
            regexp.append("]");  
        }  
        regexp.append("]");  

        String message = "ab\nc[d]e{fMardk1g(h)i}j";  
        StringBuilder result = new StringBuilder("");  
        String[] a = message.split(regexp.toString());  
        for(String string : a) {  
            result.append(string);
        }
        System.out.println(result);
        for(String str: a) System.out.print(str);
        System.out.println();
    }
}
  • 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-24T03:29:12+00:00Added an answer on May 24, 2026 at 3:29 am

    You’re using the wrong kind of grouping construct. You’re building a pattern like [xyz] which will match any single character x, y or z. You want to match any of several full strings, so you want the normal () style grouping, and the alternation operator (|). Have a look at the Pattern documentation for more details.

    Try this instead to build up the regex:

    for(String s : delimiters) {
        // We don't want to start with (|
        if (regexp.length() > 1)
        {
            regexp.append("|");
        }
        regexp.append(Pattern.quote(s));  
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this might be a very noob question, but I find that PHP Documentation
I know you might say that dictionaries are not in any order naturally, but
Typically I use E_ALL to see anything that PHP might say about my code
Before I start with the real question, let me just say that I might
This might seem like a stupid question I admit. But I'm in a small
This might be on the discussy side, but I would really like to hear
I saw this post Ruby on Rails - Awesome nested set plugin but I
I appreciate that this may be a pretty daft question, but can anyone think
I might be getting myself confused here but say I want a service to
For example, when you submit a post it might say submitted 1 second ago.

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.