Solved

Java regex

Posted on 2008-11-02
6
382 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 59

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 59

Expert Comment

by:Kevin Cross
ID: 22862893
You are welcome!
0
Industry Leaders: 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!

 

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 59

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

Independent Software Vendors: 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
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 …
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…

740 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