lundi 28 novembre 2016

Find words with pattern on java code

I have the following code, trying to extract two words(The ball) based on a regex(regex()), but matcher does not find these words. Can you help me?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class change1 {
private static String[] sentence_to_array;
public static void main(String[] args) {
    String sentence = "The ball is round";
    sentence_to_array = sentence.split(" ");        
    Pattern p = null;
    Matcher m = null;
    String to_remove = findings(p, m, sentence_to_array, regex());
}//method
private static String findings(Pattern p, Matcher m, String[] pieces, String fr) {
    String word = "";
    pieces = sentence_to_array;
    p = Pattern.compile(fr);
    for (String piece : pieces) {
        m = p.matcher(piece);
        if (m.find()) {
            word = word.concat(piece + " ");
        }//if
    }//for
    return word;
}//method
public static String first_regex() {
    return "(The|or|what)";
}//Method
public static String second_regex() {
    return "(Peter|Luke|Hans|ball)";
}//method
public static String regex() {//επιστρέφει το υποκείμενο ως regex
    return "(" + first_regex() + " " + second_regex() + ")";
}//method
}//class

Aucun commentaire:

Enregistrer un commentaire