Solved

Java regex

Posted on 2008-11-02
6
378 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

778 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