2014-10-27 105 views
-2

我需要从房屋号码/单元号码中提取号码范围。 例 - 图1B〜36B, 1B到36B或 B1至B36或 乙1至B 36 结果应为1至36房屋号码拆分aplhanumeric字符串

的前缀或后缀的字符或数字可具有任何长度。 ex B150或B1709或150B或 150Block或1709Block

请让我知道如何在Java中实现这一点。

谢谢。

+0

当然,它*可以*在Java中实现。 – Celeo 2014-10-27 23:00:57

+0

可能的重复[获取字符串“600sp”的整数部分的最佳方式?](http://stackoverflow.com/questions/3552756/best-way-to-get-integer-part-of-the-string- 600sp) – MarsAtomic 2014-10-27 23:03:53

回答

0

使用正则表达式来执行此操作。具体看字符类,量词,组和捕捉。

这里是链接到cheatsheet

为了让你在这里开始是一个“类似”的例子。

String line = "36-B"; 
Pattern pattern = Pattern.compile("(\\d*)(.+)(\\d*)"); 
Matcher matcher = pattern.matcher(line); 

while (matcher.find()) 
{ 
    System.out.println("First captured group: " + matcher.group(1)); 
    System.out.println("Second captured group: " + matcher.group(2)); 
    System.out.println("Thisr captured group: " + matcher.group(3)); 
} 

让我们知道您是否遇到问题。快乐编码!

+0

我认为string.replaceAll(“[^ 0-9]”,“”)适用于我所能想到的所有情况,以捕获数值。 – user3716891 2014-10-28 02:04:16

+0

这个正则表达式也可以正常工作。 – Chiseled 2014-10-28 02:06:24

+0

如果您满意,请将答案标记为已接受。 – Chiseled 2014-10-28 02:07:21