Solved

Java regex

Posted on 2008-11-02
6
383 Views
Last Modified: 2013-11-23
Well, replacement and pattern are two different strings.

String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";

However, they both have a similar pattern which is mmmm.nnn?ooo equal to a number. My question is asking how to get the number (9876) from the string pattern and replace the number (45302) of the string replacement i.e. to change the string replacement number (45302)  to 9876. Then the new string should be AAA mmmm.nnn?ooo=9876 EEEEEE FFFFF GGGGGG.

The regex = "mmmm.nnn\\?ooo=[0-9]+" can get the first step right to yield mmmm.nnn?ooo=9876 from the string pattern. The next step is we need to do substitution. How can we replace the mmmm.nnn?ooo=45302 from the string replacement with mmmm.nnn?ooo=9876 in order to have a new string AAA mmmm.nnn?ooo=9876 EEEEEE FFFFF GGGGGG.

Hope my question is more clear for you :)

More detail can be found from the link provided below.

http://www.experts-exchange.com/Programming/Languages/Java/New_to_Java/Q_23868181.html#a22862269
0
Comment
Question by:bin571
[X]
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
6 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22862542
This should handle that:
String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
        String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
             
        Pattern p = Pattern.compile("mmmm.nnn\\?ooo=[0-9]+"); 
        Matcher m = p.matcher(pattern);
        if (m.find()) {
            String s = m.group();
            // JOptionPane.showMessageDialog(null, s);
            m = Pattern.compile("mmmm.nnn\\?ooo=[0-9]+").matcher(replacement);
            
            if (m.find()) {
               // JOptionPane.showMessageDialog(null, m.group());
               replacement = m.replaceAll(s);
            }
JOptionPane.showMessageDialog(null, replacement);
}

Open in new window

0
 

Author Comment

by:bin571
ID: 22862856
Excellent!!! Thanks a lot!!!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22862893
You are welcome!
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:bin571
ID: 22863463
Hello mwvisa1, I am very sorry to bother you again. Please give me a hand.

If the pattern replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG" changes to
"AAA mmmm.nnn?ooo=9877 EEEEEE FFFFF GGGGGG", it is possible to do that?

PS: 9877 = 9876 + 1 (String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";)
In other words, I want the new pattern always plus one from the matching pattern. Please help!!!

My thought is using substring to get the value of 9876 from mmmm.nnn?ooo=9876, convert the string 9876 to integer and plus 1. Then we have mmmm.nnn?ooo=9877. But I don't how to replace this back to the string replacement :(
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22863522
See my notes in code.

String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
        String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
             
        Pattern p = Pattern.compile("mmmm.nnn\\?ooo=[0-9]+"); 
        Matcher m = p.matcher(pattern);
        if (m.find()) {
            String s = m.group();
// INSERT NEW CODE HERE
// SUBSTRING LEFT OF = AND CONCATENATE WITH RIGHT OF EQUAL AFTER YOU ADD 1 TO IT AS A NUMBER
            // JOptionPane.showMessageDialog(null, s);
            m = Pattern.compile("mmmm.nnn\\?ooo=[0-9]+").matcher(replacement);
            
            if (m.find()) {
               // JOptionPane.showMessageDialog(null, m.group());
               replacement = m.replaceAll(s);
            }
JOptionPane.showMessageDialog(null, replacement);
}

Open in new window

0
 

Author Comment

by:bin571
ID: 22864129
got it. thank you very much!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

710 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