如何正确导入 libphonelib 到 java 项目?

How to properly import the libphonelib to the java project?

我正在尝试使用 libphonenumber 库中的 PhoneNumberMatcher。将 jar 文件添加到我的项目并设置 BuildPath 后,我能够将 classes 导入到我的项目中:

import com.google.i18n.phonenumbers.*;

在库中,有一个名为 PhoneNumberMatcher.class 的 class。我一直在尝试找到它,但是这个 class 名称不包含在我按 Ctrl + Space.[=15= 时通常得到的建议中]

如果我坚持写名字 class,eclipse 会在名字和消息 "The type PhoneNumberMatcher is not visible". 下划线 最近我意识到 class 在项目资源管理器中有一个小蓝旗图标。

它不是唯一一个有这样一个蓝旗的,然后我尝试了其他 classes 并且我意识到所有带有这个蓝旗的 classes 都不可访问。这就是为什么我认为这些 classes 可能是私有的,或者供 lib 内部使用。

我正在尝试创建一个工具来从文本中提取 phone 个数字,我读到这个库正是为此而准备的。

请问如何在我的 java 项目中使用 PhonNumberMatcher class?

 CharSequence text = "Call me at +1 425 882-8080 for details.";
 String country = "US";
 PhoneNumberUtil util = PhoneNumberUtil.getInstance();

 // Find the first phone number match:
 PhoneNumberMatch m = util.findNumbers(text, country).iterator().next();

 // rawString() contains the phone number as it appears in the text.
 "+1 425 882-8080".equals(m.rawString());

 // start() and end() define the range of the matched subsequence.
 CharSequence subsequence = text.subSequence(m.start(), m.end());
 "+1 425 882-8080".contentEquals(subsequence);

 // number() returns the the same result as PhoneNumberUtil.parse()
 // invoked on rawString().
 util.parse(m.rawString(), country).equals(m.number());

https://javadoc.io/doc/com.googlecode.libphonenumber/libphonenumber

谢谢你的回答,Chana。 我确实能够使用该库,但后来我意识到该库对我来说使用起来太复杂了,所以我编写了自己的代码来提取德国 phone 号码、IBAN、邮政编码和金额,然后然后分类:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberExtractorAndClassifier {

public static final String REGEXPhoneNumbers = "(0|0049\s?|\+49\s?|\(\+49\)\s?|\(0\)\s?){1}([0-9]{2,4})([ \-\/]?[0-9]{1,10})+";
public static final String REGEXIbanNumbers = "([A-Z][ -]?){2}([0-9]([ -]?)*){12,30}";
public static final String REGEXAmounts = "(\d+([.,]?)\d{0,}([.,]?)\d*(\s*)(\$|€|EUR|USD|Euro|Dollar))|((\$|€|EUR|USD|Euro|Dollar)(\s*)\d+([.,]?\d{0,}([.,]?)\d*))";
public static final String REGEXPostCode = "\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b";
public static String TextToAnalyze;
public static String CopyOfText = "";
public static int ExaminatedIndex = 0;
public static List<String> ExtractedPhoneNumbers = new ArrayList<String>();
public static List<String> ExtractedIbanNumbers = new ArrayList<String>();
public static List<String> ExtractedAmounts = new ArrayList<String>();
public static List<String> ExtractedPostCodes = new ArrayList<String>();
public static final String EMPTY_STRING = "";


/**
 * @brief Constructor: initializes the needed variables and call the different methods in order to find an classify the Numbers
 * 
 * @param Text: is the input text that need to be analyzed 
 */
public NumberExtractorAndClassifier(String Text) {
    
    TextToAnalyze = Text;    //- This variable is going to have our complete text
    CopyOfText = Text;       //- This variable is going to have the missing text to analyze
    
    //- We extract the amounts first in order to do not confuse them later with a phone number, IBAN or post-code
    ExtractedAmounts = ExtractAmounts();  
    for (String Amount : ExtractedAmounts)
    {
        //- We cut them out of the text in order to do not confuse them later with a IBAN, phone number or post-code
        String safeToUseInReplaceAllString = Pattern.quote(Amount);
        CopyOfText = CopyOfText.replaceAll(safeToUseInReplaceAllString, "");
        System.out.println("Found amount -------> " + Amount);
    }
    
    //- We extract the IBAN secondly in order to do not confuse them later with a phone number or post-code
    ExtractedIbanNumbers = ExtracIbanNumbers();
    for (String Iban : ExtractedIbanNumbers)
    {
        //- We cut them out of the text in order to do not confuse them later with a phone number, or post-code
        String safeToUseInReplaceAllString = Pattern.quote(Iban);
        CopyOfText = CopyOfText.replaceAll(safeToUseInReplaceAllString, "");
        System.out.println("Found IBAN ---------> " + Iban);
    }
            
    //- We extract the phone numbers thirdly in order to do not confuse them later with a post-code
    ExtractedPhoneNumbers = ExtractPhoneNumbers();
    for( String number : ExtractedPhoneNumbers )
    {
        //- We cut them out of the text in order to do not confuse them later with a post-code
        String safeToUseInReplaceAllString = Pattern.quote(number);
        CopyOfText = CopyOfText.replaceAll(safeToUseInReplaceAllString, "");
        System.out.println("Found number -------> " + number);
    }
    
    ExtractedPostCodes = ExtractPostCodes();
    for( String PostCode : ExtractedPostCodes)
    {
        System.out.println("Found post code ----> " + PostCode);
    }

}




/**
 * @Brief Method extracts phone numbers out of the text with help of REGEXPhoneNumbers
 * 
 * @return List of strings with all the found numbers.
 */
public static List<String> ExtractPhoneNumbers(){
    //Initializing our variables
    List<String> FoundNumbers =  new ArrayList<String>();
    boolean LineContainsNumber = true;
    Pattern pattern = Pattern.compile(REGEXPhoneNumbers);
    Matcher matcher = pattern.matcher(CopyOfText);
    while (LineContainsNumber) {
        if (matcher.find()) {
            String NumberFoundByTheMatcher = matcher.group(0);
            FoundNumbers.add(NumberFoundByTheMatcher);
        }
        else{LineContainsNumber = false;}
    }
    return FoundNumbers;
}



/**
 * @Brief Method extracts IBAN numbers out of the text with help of REGEXIbanNumbers
 * 
 * @return List of strings with all the found IBANS numbers.
 */
public static List<String> ExtracIbanNumbers(){
    //Initializing our variables
    List<String> FoundIbans =  new ArrayList<String>();
    boolean LineContainsIban = true;
    Pattern pattern = Pattern.compile(REGEXIbanNumbers);
    Matcher matcher = pattern.matcher(CopyOfText);
    while (LineContainsIban) {
        if (matcher.find()) {
            String NumberFoundByTheMatcher = matcher.group(0);
            FoundIbans.add(NumberFoundByTheMatcher);
        }
        else{LineContainsIban = false;}
    }
    return FoundIbans;
}


/**
 * @Brief Method extracts Amounts out of the text with help of REGEXAmounts
 * 
 * @return List of strings with all the found amounts.
 */
public static List<String> ExtractAmounts(){
    //Initializing our variables
    List<String> FoundAmounts =  new ArrayList<String>();
    boolean LineContainsAmount = true;
    Pattern pattern = Pattern.compile(REGEXAmounts);
    Matcher matcher = pattern.matcher(CopyOfText);
    while (LineContainsAmount) {
        if (matcher.find()) {
            String NumberFoundByTheMatcher = matcher.group(0);
            FoundAmounts.add(NumberFoundByTheMatcher);
        }
        else{LineContainsAmount = false;}
    }
    return FoundAmounts;
}


/**
 * @Brief Method extracts post codes out of the text with help of REGEXPostCode
 * 
 * @return List of strings with all the found post codes.
 */
public static List<String> ExtractPostCodes(){
    List<String> FoundPostCodes = new ArrayList<String>();
    boolean LineContainsPostCode = true;
    Pattern pattern = Pattern.compile(REGEXPostCode);
    Matcher matcher = pattern.matcher(CopyOfText);
    while(LineContainsPostCode) {
        if(matcher.find()) {
            String PostCodeFoundByMatcher = matcher.group(0);
            FoundPostCodes.add(PostCodeFoundByMatcher);
        }
        else {
            LineContainsPostCode = false;
        }
    }
    return FoundPostCodes;
}

}