Solved

Java regex

Posted on 2008-11-02
6
376 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
Comment Utility
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
Comment Utility
Excellent!!! Thanks a lot!!!
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
You are welcome!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bin571
Comment Utility
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
Comment Utility
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
Comment Utility
got it. thank you very much!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 46
endX challenge 2 48
topping3 challenge 14 48
github account with ecipse 1 15
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now