Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Regex problem!

Posted on 2008-10-26
9
Medium Priority
?
399 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 61

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 61

Expert Comment

by:Kevin Cross
ID: 22809482
0
 
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 61

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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 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