Solved

Java Regex problem!

Posted on 2008-10-26
9
374 Views
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!
0
Comment
Question by:AnqiaWu
  • 3
  • 3
  • 3
9 Comments
 
LVL 1

Expert Comment

by:Phasmid
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]*)");

		System.out.println(q_23848769.replace(regex,"$1KMG$2"));

  }
 

	private String replace(Pattern regex, String replace) {

		Matcher matcher = regex.matcher(this._string);

		return matcher.replaceAll(replace);

  }
 

}

Open in new window

0
 
LVL 59

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.

Regards,
Kevin
0
 

Author Comment

by:AnqiaWu
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.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22809482
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:Phasmid
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.
0
 

Author Comment

by:AnqiaWu
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.........
0
 

Accepted Solution

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

Expert Comment

by:Phasmid
ID: 22816254
Charming!  It's people like you that make helping out so much fun!
0
 
LVL 59

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!

/kev
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

943 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now