Java Regex problem!

Posted on 2008-10-26
Medium Priority
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.
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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! www.experts-exchange.com 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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