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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:22:38+00:00 2026-05-24T01:22:38+00:00

I have a complete Java based code base, where members are named: String m_sFoo;

  • 0

I have a complete Java based code base, where members are named:

String m_sFoo;
Array m_arrKeepThings;

Variable/object names includes both a m_ prefix to indicate a member, and an hungarian notation type indicator.

I’m looking for a way to perform a single time code replacment to (for example on the above to cases):

Array keepThings;
String foo;

Of course there are many other alternatives, but I hope that based on two examples, I’ll be able to perform the full change.
Performances is not an issue as it’s a single time fix.

To clarify, if I had to explain this in lines, it would be:

  1. Match words starting with m_[a-zA-Z].
  2. After m_, drop whatever is there before the first Capital letter.
  3. Change the first capital letter to lower case.
  • 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-24T01:22:39+00:00Added an answer on May 24, 2026 at 1:22 am

    Here is some Java code that works. It is not pure regex, but based on:

    Usage:

    String str = "String m_sFoo;\n"
            + "Array m_arrKeepThings;\n"
            + "List<? extends Reader> m_lstReaders; // A silly comment\n"
            + "String.format(\"Hello World!\"); /* No m_named vars here */";
    // Read the file you want to handle instead
    
    NameMatcher nm = new NameMatcher(str);
    System.out.println(nm.performReplacements());
    

    NameMatcher.java

    package so_6806699;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     *
     * @author martijn
     */
    public class NameMatcher
    {
    
        private String input;
        public static final String REGEX = "m_[a-z]+([A-Z0-9_\\$\\µ\\£]*)";
        public static final Pattern PATTERN = Pattern.compile(REGEX);
    
        public NameMatcher(String input)
        {
            this.input = input;
        }
    
        public String performReplacements()
        {
            Matcher m = PATTERN.matcher(input);
            StringBuilder sb = new StringBuilder();
    
            int oldEnd = 0;
            while (m.find())
            {
                int start = m.start();
                int end = m.end();
    
                String match = input.substring(start, end);
                String matchGroup1 = match.replaceAll(REGEX, "$1");
                if (!matchGroup1.isEmpty())
                {
                    char[] match_array = matchGroup1.toCharArray();
                    match_array[0] = Character.toLowerCase(match_array[0]);
                    match = new String(match_array);
                }
    
                sb.append(input.substring(oldEnd, start));
                oldEnd = end;
    
                sb.append(match);
            }
            sb.append(input.substring(oldEnd));
            return sb.toString();
        }
    }
    

    Demo Output:

    String foo;
    Array keepThings;
    List<? extends Reader> readers; // A silly comment
    String.format("Hello World!"); /* No m_named vars here */
    

    Edit 0:
    Since dollar signs ($), micro (µ) and pound (£) are valid characters for Java name variables, I edited the regex.

    Edit 1: It seems that there are a lot of non-latin characters that are valid (éùàçè, etc). Hopefully you don’t have to handle them.

    Edit 2: I’m only a human being! So be aware of errors there might be in the code! Make a BACKUP first!

    Edit 3: Code improved. A NPE was thrown when the code contains this: m_foo. These will be unhandled.

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

Sidebar

Related Questions

I have a complete XML document in a string and would like a Document
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have a Windows CE device that we are deploying, but we have complete
We have an auto-complete list that's populated when an you send an email to
I have to rename a complete folder tree recursively so that no uppercase letter
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have a series of forms that I need a user to complete in
I get a .pdf complete with images, fancy fonts, styles, gradients and what have
I'm starting a personal project, so I have at the moment complete architectural/design control.
I have developed a java program as a part of my homework and have

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.