简体   繁体   中英

Using Multiple Java regular expressions

I am trying to extract an email and replace it with a space using a pattern(EMAIL_PATTERN). When running the following, no output is produced when a full document is passed in. The pattern will only match the entire region. So this means if we pass in only the email, the email will be matched and be replaced with a space. But the purpose of the following method is to find the email and previous manual extraction is not required. After the email in the tempString has been replaced, I want to use it for the next pattern. Should I combine the patterns I want to use in one method or should they be placed in separate methods? Below is the code I have as of now. Also I have other patterns, but since my method is not working correctly I have not posted them yet.

private static final String EMAIL_PATTERN = "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
public static void main (String[] args) {
//Document takes in a ID, student information(which includes email, address, phone, name),   school, and text   
Document r = new Document("", "FirstName LastName, Address, example@email.com,    phoneNumber", "School", "experience", "text");
            personalEmailZone(r);

 }
public static Document personalEmailZone(Document doc){
    //tempString is the personal information section of a resume
    String tempPI = doc.tempString();
    if(doc.tempString().matches(EMAIL_PATTERN) == true){
        //Pattern pattern = Pattern.compile("");
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(tempPI);
        String emailTemp = "";
        if(matcher.find()){
            emailTemp = matcher.group();
            System.out.println(emailTemp);
            //PI.replace(emailTemp, "");
            System.out.println(emailTemp.replace(emailTemp, ""));
            tempPI = tempPI.replace(emailTemp, "");
            System.out.println(tempPI);
        }
    }
    return doc;
}

You can place your patterns in different methods, which return the modified string for the text pattern usage. For example

String tempPI = doc.tempString();
tempPI = applyPattern1(tempPI);
tempPI = applyPattern2(tempPI)
tempPI = applyPattern3(tempPI);

Your code does't show any output because of doc.tempString().matches(EMAIL_PATTERN) == true . Maybe it's not needed there, since it expects the entire string to be an email.

You have several problems:

public static Document personalEmailZone(Document doc){
    //tempString is the personal information section of a resume
    String tempPI = doc.tempString();
    if(doc.tempString().matches(EMAIL_PATTERN) == true){

The above statement attempts to match the entire document against the email address pattern. This will not match unless doc.tempString() contains ONLY a single email address and nothing else.

        //Pattern pattern = Pattern.compile("");
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(tempPI);
        String emailTemp = "";
        if(matcher.find()){
            emailTemp = matcher.group();
            System.out.println(emailTemp);
            //PI.replace(emailTemp, "");
            System.out.println(emailTemp.replace(emailTemp, ""));

Not sure what the above is for. If your code ever reached this point, it would always print an empty line.

            tempPI = tempPI.replace(emailTemp, "");
            System.out.println(tempPI);
        }

Since there's no loop, you will have replaced only the first occurrence of an email address. If you're expecting to replace ALL occurrences, you need to loop over the input.

    }
    return doc;

At this point you haven't actually modified doc , so you're returning the document in its original form, with email addresses included.

}

Look at the Javadoc for String#replaceAll(String regex, String replacement)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM