Solved

Java Regex problem!

Posted on 2008-10-26
9
372 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:Phasmid
Comment Utility
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
Comment Utility
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
Comment Utility
Don't bother to send me any suggestions. I already solved the problem by myself.
0
 
LVL 1

Expert Comment

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

Expert Comment

by:Kevin Cross
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 47
ArrayLists in ArrayLists/HashMaps? 4 38
Modeling a class in java 5 31
Python Assistance 7 30
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

6 Experts available now in Live!

Get 1:1 Help Now