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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:44:47+00:00 2026-06-10T18:44:47+00:00

I am not good at regex and I am trying to use java.lang.String replaceAll()

  • 0

I am not good at regex and I am trying to use java.lang.String replaceAll() method with code segments as follows

A is:

public class method3 {

    public static int addTwoNumbers(int one, int two){
        return one+two;
    }

    public static void main (String[] args){
        int total = addTwoNumbers(1, 3);
        System.out.println(total);
    }
}

to replace with B

null
public class method3 {
    /* some writting */
    public static int addTwoNumbers(int one, int two){
        return one+two;
    }
    /*more text*/
    public static void main (String[] args){
        int total = addTwoNumbers(1, 3);
        System.out.println(total);
    }//end of 
}

And this is giving me the following error

java.util.regex.PatternSyntaxException: Illegal repetition

Im guessing this has something to do with the /**/ characters?

How do you sort this and is there any other characters i need to look out for?

EDIT: This is the error message

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal repetition near index 25
null
public class method2 {

    public static int addTwoNumbers(int one, int two){
        return one+two;
    }

    public static void main (String[] args){
        int total = addTwoNumbers(1, 3);
        System.out.println(total);
    }
}


                         ^
    at java.util.regex.Pattern.error(Pattern.java:1924)
    at java.util.regex.Pattern.closure(Pattern.java:3104)
    at java.util.regex.Pattern.sequence(Pattern.java:2101)
    at java.util.regex.Pattern.expr(Pattern.java:1964)
    at java.util.regex.Pattern.compile(Pattern.java:1665)
    at java.util.regex.Pattern.<init>(Pattern.java:1337)
    ....

call to replaceAll()

        String a = readFile(directoryToAddFile,startOfCode, endOfCode);
        String b = textarea.getText().toString();
        String content = IOUtils.toString(new FileInputStream(directoryToAddFile));
        content = content.replaceAll(a, b);
        IOUtils.write(content, new FileOutputStream(directoryToAddFile));
  • 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-06-10T18:44:49+00:00Added an answer on June 10, 2026 at 6:44 pm

    Currently, your code attempts to analyze the “to replace” string as a regex.

    To replace a string literally rather than as a regex, you can use Pattern#quote(String).

    content = content.replaceAll(Pattern.quote(a), b);
    

    Also, as a side note, you might find String#replace(CharSequence, CharSequence) more appropriate than replaceAll in this situation.

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

Sidebar

Related Questions

I'm not very good at RegEx, can someone give me a regex (to use
Hey So I am not good with regex right now, trying to learn though,
I'm not so good with regex, and need some help. I have a string
I'm not really that good with regex, but I understand the basics. I'm trying
Hi I'm not particular good at Java so please bear with me. I'm trying
I'm not very good at regex but maybe there's a simple way to achieve
I am not so good with regex. I am struggling to find a solution
I'm not good at t-sql, so I need help. I have this code I
Hey I'm trying to use a regex to count the number of quotes in
I'm trying to extract the year from a timestamp using regex, but I'm not

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.