2016-12-01 45 views
-3

工作Android上的OCR项目,在一个条件扫描文本格式 出字符串后的从单个字符串中提取传真号码或电话号码?

Tel:+91 345677890 Fax: +91 80 222767000 

中需要提取唯一的电话号码和传真号码。

在第二个例子

[email protected] ,Fax:+91 80 222767000 

我需要在两个传真,电话和电子邮件分隔成一个变量

这篇但无法找到解决方案

String cellfound="Tel:+91 345677890 Fax: +91 80 222767000 [email protected]"; 
Pattern cellp1= Pattern.compile(".*\\b(Mobile|M|M)\\b.*",Pattern.CASE_INSENSITIVE); 
Matcher cellm1 = cellp1.matcher(cellnumber); 
if (cellm1.matches()) { 
    cellfound=cellm1.group(); 
    System.out.println("\nbefore cell found "+cellfound); 
    cellfound=cellfound.replaceAll("[^0-9]", " "); 
    System.out.println("\nfinal cell found from pattern :"+cellfound); 
} 
+0

使用String.subString()方法来单独数据 –

+3

听起来像是正则表达式的工作。 – Biffen

+0

使用正则表达式作为电话号码应该是一个正则表达式。 – Antoniossss

回答

1

这将工作对你而言:

public static void main(String[] args) throws Exception { 
    String s ="Tel:+91 345677890 Fax: +91 80 222767000"; 
    String[] arr = s.split("[a-zA-Z:]+\\s*"); 
    for (String str : arr){ 
     System.out.println(str); 
    } 

    String s2 = "[email protected] ,Fax:+91 80 222767000"; 
    arr = s2.split(",\\w+:"); 
    for (String str : arr){ 
     System.out.println(str); 
    } 
} 

O/P:

<empty String here> // ignore this value 
+91 345677890 
+91 80 222767000 
[email protected] 
+91 80 222767000 
0

你可以试试这个:

(?<=Tel[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)|(?<=Fax[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)|(?<=\\s)(\\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,}\\b) 

说明:

(?<=Tel[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)比赛空间由“电话之前组:“ - >这个捕获电话号码。

(?<=Fax[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)匹配空间组由先“传真” - >这抓住了传真号码

,最后一个(?<=\\s)(\\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,}\\b)是前面有一个空格的电子邮件正则表达式。

正如你所看到的,电话传真有几乎相同的正则表达式。我们可以将它合并为一个,但我想分开它以获得更清晰的结果。

下面是示例代码:

import java.util.regex.*; 

public class HelloWorld { 
    public static void main(String []args){ 
     String test = "Tel:+91 345677890 Fax: +91 80 222767000 [email protected]"; 

     String regex = "(?<=Tel[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)|" // this captures the tel number 
       + "(?<=Fax[:\\s])([+\\d\\s]+\\S)(?=\\s\\D)|" // this captures the fax number 
       + "(?<=\\s)(\\b[A-Z0-9._%+-][email protected][A-Z0-9.-]+\\.[A-Z]{2,}\\b)"; // this captures the email string 

     // Remember the CASE_INSENSITIVE option 
     Pattern re = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 

     Matcher m = re.matcher(test); 
     while (m.find()) { 
      System.out.println(m.group(0).trim()); 
     } 
    } 
} 

预期的结果是这样的:

+91 345677890                                                      
+91 80 222767000                                                     
[email protected]