【问题标题】:How to properly import the libphonelib to the java project?如何将libphonelib正确导入java项目?
【发布时间】:2020-06-01 14:41:14
【问题描述】:

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

import com.google.i18n.phonenumbers.*;

在 lib 内部,有一个名为 PhoneNumberMatcher.class 的类。我一直在尝试找到它,但是当我按 Ctrl + Space 时,我通常会收到的建议中不包含此类名称。

如果我坚持写名字类,eclipse会在名字和消息下划线“类型PhoneNumberMatcher不可见”。 最近我意识到项目资源管理器中的类有一个小的蓝色标志图标。

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

我正在尝试创建一个从文本中提取电话号码的工具,我读到这个库正是为它准备的。

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

【问题讨论】:

    标签: java eclipse import libphonenumber


    【解决方案1】:
     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

    【讨论】:

    • 这没有回答问题的“PhoneNumberMatcher 类型不可见”部分
    【解决方案2】:

    感谢您的回答 Chana。 我确实能够使用这个库,但后来我意识到这个库对我来说太复杂了,所以我编写了自己的代码来提取德国电话号码、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;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2021-11-06
      • 2012-08-12
      • 2023-03-15
      • 1970-01-01
      • 2013-06-03
      • 2022-01-21
      相关资源
      最近更新 更多