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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:21:40+00:00 2026-05-10T20:21:40+00:00

A string will be made up of certain symbols (ax,bx,dx,c,acc for example) and numbers.

  • 0

A string will be made up of certain symbols (ax,bx,dx,c,acc for example) and numbers.

ex: ax 5 5 dx 3 acc c ax bx

I want to replace one or all of the symbols (randomly) with another symbol of the same set. ie, replace one of {ax,bx,dx,c,acc} with one of {ax,bx,dx,c,acc}.

replacement example: acc 5 5 dx 3 acc c ax bx or c 5 5 dx 3 acc c ax ax

Is there a way to do this with regexes? In Java? If so, which methods should I use?

  • 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. 2026-05-10T20:21:41+00:00Added an answer on May 10, 2026 at 8:21 pm

    I think this is the most clean solution for replacing a certain set of symbols from a string containing a superset of them. appendreplacement is the key to this method. one important caveat: do not include any unescped dollar characters ($) in your elements list. escape them by using ‘\$’ eventually use
    .replaceall(‘\$’,’\\$’); on every string before adding it to the list. see also the javadoc in doubt about the $ signs.

    import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher;  public class ReplaceTokens { public static void main(String[] args) {     List<String> elements = Arrays.asList('ax', 'bx', 'dx', 'c', 'acc');     final String patternStr = join(elements, '|'); //build string 'ax|bx|dx|c|acc'      Pattern p = Pattern.compile(patternStr);     Matcher m = p.matcher('ax 5 5 dx 3 acc c ax bx');     StringBuffer sb = new StringBuffer();     Random rand = new Random();     while (m.find()){         String randomSymbol = elements.get(rand.nextInt(elements.size()));         m.appendReplacement(sb,randomSymbol);     }     m.appendTail(sb);     System.out.println(sb); }  /**  * this method is only needed to generate the string ax|bx|dx|c|acc in a clean way....  * @see org.apache.commons.lang.StringUtils.join    for a more common alternative...  */ public static String join(List<String> s, String delimiter) {     if (s.isEmpty()) return '';     Iterator<String> iter = s.iterator();     StringBuffer buffer = new StringBuffer(iter.next());     while (iter.hasNext()) buffer.append(delimiter).append(iter.next());     return buffer.toString(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: /** * This function will determine whether or not one string starts with
I want a bash function to count for the String Width that will comsumed
When you want a certain task to be executed by another thread, you can
I have the following string which will probably contain ~100 entries: String foo =
I need a function to return a string that will only be accessed read-only.
Per the documentation , String.Format will throw a FormatException if either (A) the format
I am using rsa key to encrypt a long string which I will send
How would I extract the dollar amount from the following string some text will
this code will not pass the query string to default3.aspx on Image Button ..
imagine if you will the following string: A Great Movie (1999) DVDRip.avi I am

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.