Java Regex problem!

Posted on 2008-10-26
Last Modified: 2013-11-23
Given 2 patterns -
1. 1234 567890 123456 7890 12 3456 78ABC901 23456 7890
2. 9457 837434 373495 4557 34 4585 35KMG784 67677 5676

How can I use regex to replace the "ABC"  to "KMG"?
The result is 1234 567890 123456 7890 12 3456 78KMG901 23456 7890

PS:You must use regex class, i.e. [0-9]*[A-Z]*[0-9]* to match the pattern 2 or you can use [A-Z] as long as works!!! Thanks in advance!
Question by:AnqiaWu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3

Expert Comment

ID: 22808206
Does this do if for you?
package ee.Java.Q_23848769;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Q_23848769 {
	private final String _string;
	public Q_23848769(String string) {
		this._string = string;
	public static void main(String[] args) {
	  Q_23848769 q_23848769 = new Q_23848769("1234 567890 123456 7890 12 3456 78ABC901 23456 7890");
	  Pattern regex = Pattern.compile("([0-9]*)ABC([0-9]*)");
	private String replace(Pattern regex, String replace) {
		Matcher matcher = regex.matcher(this._string);
		return matcher.replaceAll(replace);

Open in new window

LVL 60

Expert Comment

by:Kevin Cross
ID: 22808615
AnqiaWu, since this appears to be homework, I would suggest you research the API's used above for java.util.regex and take a stab at doing what you requested as I think it is slightly different than what is shown.

If I am understanding correctly, you don't know the "KMG" portion ahead of time, it must be extracted from pattern 2 and then used in the replace of pattern 1.

Give that a try and post back with specific issues you are getting as we can give direction on homework, but NOT final solutions here on EE.  And for your benefit this is best as you will hopefully learn how to do it for yourself which usually helps for tests/class versus being given code that you may not understand or uses advanced API that teacher didn't want you to implement yet.


Author Comment

ID: 22809396
Here is the problem (replace(regex,"$1KMG$2")). We cannot hard code the KMG because it can be any upper case alphabet strings, for example. ROBH. Thats why I suggest this regex class [0-9]*[A-Z]*[0-9]*. We may want to use this regex class to get the pattern from pattern 2 i.e.KMG. Then we can use this pattern to replace the ABC.

Hello mwvisa1,
I have been working on unix/linux and programming C over 20 years. If I allow to use shell script to do the regex for my current project, it can be done within a minute. I just new to java and don't know the syntax.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

LVL 60

Expert Comment

by:Kevin Cross
ID: 22809482

Expert Comment

ID: 22811923
Now, that you've clarified the problem, you should be able to modify the code I presented earlier and use that.  It has what you need to understand how Java does regex.  There's also a programmatic way to get the matched groups - see the links mwvisa1 provided.

Author Comment

ID: 22816185
What the heck both of you! I am looking for solution. Let me make it clear! is not a learning center or school. Can you tell me why people spend money to buy points and asking questions? The purpose is looking for a solution.

Here is my comments:
1. Phasmid, you don't even understand my question.
2. mwvisa1, why don't you tell me go to library to get a java reference book to find a solution.

You guys are wasting people's time. Boooo.........

Accepted Solution

AnqiaWu earned 0 total points
ID: 22816225
Don't bother to send me any suggestions. I already solved the problem by myself.

Expert Comment

ID: 22816254
Charming!  It's people like you that make helping out so much fun!
LVL 60

Expert Comment

by:Kevin Cross
ID: 22816942
Then educate the masses with the solution and accept your solution as the answer, so your points will be refunded back to you.

Sorry you got so frustrated.  I am too use to getting students in this zone, so I post in a manner for collaboration on the solution versus me posting the code.  Sorry that offended you.  My response with links was due to the fact you indicated you were a development pro.  A lot of times folks with your development ability don't want an answer fed to them, they want to learn how to do it too...probably more so than the students as you already should have the logic experience and all you need is reference on syntax which is what I gave you.

I don't want to waste my time writing you code you don't want either. :) Sorry you saw this as a waste of your time.

Anyway, best regards.

Happy coding!


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 78
Html Table Looping (part 2) 5 35
Setup GlassFish 4 37
How to log java errors in tomcat 8 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question