Solved

Java regex

Posted on 2008-11-01
15
344 Views
Last Modified: 2013-11-23
Can you help me to solve my problems?

1. want to get the matching pattern (String pattern), the expected output should be mmmm.nnnn?ooo=9876
2. want to replace the string replacement, the expected output should be as below.
AAA mmmm.nnn?ooo=9876 EEEEEE FFFFF GGGGGG

My code doesn't work at all :(

public static void main(String[] args) {
 
    String regex = "^mmmm.nnnn?ooo=[0-9]+?"; 
    String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
    String pattern =  "KLDKFVN FDFJDVmmmm.nnn?000=9876ERFVS";
 
    Pattern p = Pattern.compile(regex);	
    Matcher m = p.matcher(pattern);
    if (m.find()) {
	pattern = m.group();
    }
    System.out.println(pattern);		
}

Open in new window

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
  • 9
  • 6
15 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22859707
Think the problem is the ?.  It is probably being evaluated as a regex operator versus the literal string you want it to be.  I will see if I can remember how to escape.  You can try \? but think have to use character code.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22859717
Also, the ^ in the beginning says match on everything except the pattern you have set.

Think you want the opposite.

Take a look here for reference:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22859816
Also in your string, you have ?000 and you are trying to match with ?ooo.  Note that one is zeros (0) and other are letter o (O)
0
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!

 

Author Comment

by:bin571
ID: 22859884
My bad! The String pattern should be  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
I tried regex = "^mmmm.nnnn?ooo=[0-9]+$" but didn't work.
I also tried to use \ as an escape character i.e. String regex = "^mmmm.nnnn\?ooo=[0-9]+?", I got syntax error :(
0
 

Author Comment

by:bin571
ID: 22859987
also tried regex = "^mmmm.nnnn\\(?\\)ooo=[0-9]+$", doesn't work :(
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860017
Think the statement needs an overhaul.  Just haven't figured out right pattern yet myself to offer you something different.  I would say take a look at the listing from the url I sent and start over from scratch.

^ == except, so don't think this will work in your case
0
 

Author Comment

by:bin571
ID: 22860192
public static void main(String[] args) {
 
    String regex = "mmmm.nnnn\\?ooo=[0-9]+";
    String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
    String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
 
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(pattern);
    if (m.find()) {
        pattern = m.group();
    }
    System.out.println(pattern);     // output ---> mmmm.nnn?ooo=9876          
}

I got the correct output for my first question. The regex = "mmmm.nnnn\\?ooo=[0-9]+" yields mmmm.nnn?ooo=9876. Now, how can we do the replacement?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860214
Try this:
public static void main(String[] args) {
        
        String regex = "mmmm.nnn\\?ooo=[0-9]+"; 
        String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
        String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
     
        Pattern p = Pattern.compile(regex); 
        Matcher m = p.matcher(pattern);
        if (m.find()) {
            pattern = m.replaceAll(replacement);
        }
        System.out.println(pattern);     // output ---> mmmm.nnn?ooo=9876           
    }

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860217
This will replace the whole thing:
 String regex = "^[^0-9]+mmmm.nnn\\?ooo=[0-9]+[^0-9]+$";

And it took me a while to realize, ^ does mean beginning of file as well as except, so see what you were trying to do. ;) Sorry!

If you wanted to get block starting with mmmm, then leave off the ^.  If you want all of it, you have to put in [^0-9]+ to tell the regex that there will be 1 or more non-digits in front of mmmm to start the string.
0
 

Author Comment

by:bin571
ID: 22860383
I double checked your solution non of them can generate a correct output. I don't know where did I get wrong!
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class TryRegex {
public static void main(String[] args) {
                
        String regex = "mmmm.nnn\\?ooo=[0-9]+"; 
        String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
        String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
     
        Pattern p = Pattern.compile(regex); 
        Matcher m = p.matcher(pattern);
        if (m.find()) {
            pattern = m.replaceAll(replacement);
        }
        System.out.println(pattern);     // output ---> mmmm.nnn?ooo=9876           
    }
}
The output of this one is "KLDKFVN FDFJDVAAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGGERFVS"
-------------------------------------------------------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class TryRegex  {
public static void main(String[] args) {
                
	String regex = "^[^0-9]+mmmm.nnn\\?ooo=[0-9]+[^0-9]+$";
        String replacement = "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG";
        String pattern =  "KLDKFVN FDFJDVmmmm.nnn?ooo=9876ERFVS";
     
        Pattern p = Pattern.compile(regex); 
        Matcher m = p.matcher(pattern);
        if (m.find()) {
            pattern = m.replaceAll(replacement);
        }
        System.out.println(pattern);     // output ---> mmmm.nnn?ooo=9876           
    }
}
The output of this one is "AAA mmmm.nnn?ooo=45302 EEEEEE FFFFF GGGGGG"
The correct output should be "AAA mmmm.nnn?ooo=9876 EEEEEE FFFFF GGGGGG" :(

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860399
I had tested all of them and replacement worked for me. I will see what you just posted though.  Just figured I would tell you that worked before I posted. ;)
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860405
Ah, well if the correct output shoudl be 9876 instead of 45302 why do you have that in the replacement string?
0
 

Author Comment

by:bin571
ID: 22862269
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 :)
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22862534
My thought:
+Do modified regex to find the number portion of pattern and save value.
+Do modified regex to find the number portion of replacement and replace wih value saved above.
+Do full regex to find entire string pattern and replace with replacement string.
Here is an example:

public static void main(String[] args) {
        
        String regex = "^[^0-9]+mmmm.nnn\\?ooo=[0-9]+[^0-9]*$";
        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);
            }
            
            m = Pattern.compile(regex).matcher(pattern);
            if (m.find()) {
                pattern = m.replaceAll(replacement);
            }
        }
        System.out.println(pattern);     // output ---> mmmm.nnn?ooo=9876           
    }

Open in new window

0
 

Author Comment

by:bin571
ID: 22862910
Please see another solution (also provided by Guru mwvisa1) at http://www.experts-exchange.com/Programming/Languages/Java/New_to_Java/Q_23868976.html. Thanks a lot!!!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
How to determine if a string is a valid SHA value 7 43
jsp insert to database example 2 59
Selenium docs api java index 3 64
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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