Solved

Java regex

Posted on 2008-11-02
6
384 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
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!

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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