سؤال

i was wondering if the issue is visible for you guys, i have searched many 'a stackoverflow post but have failed to get an answer.

I'm trying to make a matcher that will eventually match C.V's and Job openings. Now I'm starting to learn about regexes and matching using aforementioned regexes. I am trying to get a successful match to get the feel of matching to eventually match those other things I mentioned, but now even the simple stuff isn't working.

public class RunMatchSequence {

public static void main(String[] args) {
    try {



    String emailRegEx = "(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*";
    //  String emailRegEx = "(?s).*";
    Pattern pattern = Pattern.compile(emailRegEx);

     String target= "You can email me at g_andy@example.com or andy@example.net to get more info";
     String target2="You can email";
    java.util.regex.Matcher matcher; {

     matcher = pattern.matcher(target);

     while(matcher.find()) {
         System.out.println("Found a Match" + Matcher.group());
         System.out.println("Start position: " + Matcher.start()); 
         System.out.println("End position: " + Matcher.end()); 
          }}} catch (StackOverflowError e) {
              System.out.println("Stackoverflow");
                //JOptionPane.showMessageDialog(null, "Stackoverflow");
            }
}

}

This being the class that runs the pattern. what follows is the class that holds the group, this is where the StackOverflowError occurs.(thinking its something to do with infinite recursion, but I have no idea how to solve it): EDIT: I now realize what caused the infinite recursion is the return statement, but i have no idea what to put there

public class Matcher{
 public static void main(String[] args) {

     RunMatchSequence.main(args);

//implements MatchResult {
 //  Pattern p = Pattern.compile("a*b");
 //   java.util.regex.Matcher m = p.matcher("aaaaab");
// boolean b = m.matches();
//}
     }

static String group() {
// TODO Auto-generated method stub
return group();
}

static int end() {
// TODO Auto-generated method stub
return end();
}

static int start() {
// TODO Auto-generated method stub
return start();
}

}
هل كانت مفيدة؟

المحلول 2

well it turns out.... that the java library supports the matcher/pattern api and all you need to do is call matcher.group() in the printline..... and that my mistake was writing matcher.group() with a capital M which is not recognized by the API.

so these:

    static String group() {
    // TODO Auto-generated method stub
    return group();
    }

    static int end() {
    // TODO Auto-generated method stub
    return end();
    }

    static int start() {
    // TODO Auto-generated method stub
    return start();
   }

are obsolete.

نصائح أخرى

When you call Matcher.group, it crashes because the group method is recursive, but with no condition to avoid infinite calls.So the method is called and called and called...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top