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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:03:54+00:00 2026-05-25T02:03:54+00:00

I am having problem with android regex pattern. My target is to find {#}

  • 0

I am having problem with android regex pattern.

My target is to find {#} inside one sentence and replaces them with an array of String.

For example, my raw string will be

{#} students go to {#}.

My replacement array will be

[“5″,”park”]

After replacing, final string should be “5 students go to park.” My current snippet work just fine until the replacement array comes up with $ sign such as

[“$5″,”$park”]

Here is my current snippet.

 Pattern pattern = Pattern.compile("(\\{#\\})");
 String raw_q = "Pete has {#}. He bought {#}.";
 String[] var = ["$70","6 pens at $5 each"];
 String act_q = "";
 for (int m = 0; m < var.length(); m++) {
    try{
       act_q = raw_q.replaceFirst(pattern.pattern(),var[m]);
    }catch(Exception e){
       e.printStackTrace();
    }

Result is a blank string which I believe try block never get executed. Stack trace shows arrayindexoutofbound exception. I don’t really get what it’s happening. Any help would be appreciated.

Note – the length of replacement array is always equal to the number of {#} occurrences.

  • 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-25T02:03:55+00:00Added an answer on May 25, 2026 at 2:03 am

    For your problem, see the javadoc. String.replaceFirst() use Matcher.replaceFirst(), which refers to Matcher.appendReplacement():

    Note that backslashes () and dollar signs ($) in the replacement
    string may cause the results to be different than if it were being
    treated as a literal replacement string. Dollar signs may be treated
    as references to captured subsequences as described above, and
    backslashes are used to escape literal characters in the replacement
    string.

    That is, you must escape all $ and \.


    But you have other problem…. You have compile a pattern and NOT use it! replaceFirst() always compile the pattern again. Try this for better performance.

    Matcher m = pattern.matcher(raw_q);
    StringBuffer sb = new StringBuffer();
    int i = 0;
    while (m.find()) {
         m.appendReplacement(sb, var[i].replaceAll("\\$", "\\\\$"));
         i++;
    }
    m.appendTail(sb);
    act_q = sb.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a strange problem with Android 1.6 I have an application that has
I'm having a problem getting my android dev environment setup in Windows 7. I
I am having a problem with views overlapping in a RelativeLayout on Android 1.5...
On Android, I'm having a problem trying to figure out which ringtone is actually
I am having problem with download my android application from my own web server.
I have a very simple AsyncTask implementation example and am having problem in testing
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
Having a problem getting a TreeView control to display node images. The code below
Having a problem trying to create a function, as part of a BizTalk helper

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.