-2

I need to extract the number range from house number/ unit number. example - 1B to 36B, 1-B to 36-B or B1 to B36 or B 1 to B 36 the result should be 1 to 36

The prefix or postfix characters or digits can have any length. ex B150 or B1709 or 150B or 150Block or 1709Block

Please let me know how this can be achieved in Java.

Thanks.

  • Certainly it *can* be achieved in Java. – Celeo Oct 27 '14 at 23:00
  • possible duplicate of [Best way to get integer part of the string "600sp"?](http://stackoverflow.com/questions/3552756/best-way-to-get-integer-part-of-the-string-600sp) – MarsAtomic Oct 27 '14 at 23:03

1 Answers1

0

Use Regular expression to do this. Specifically look at character classes , quantifiers , groups and capturing.

Here is a link to the cheatsheet .

To get you started here is a "similar" example.

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));
}

Let us know if you run into issues. Happy coding !!

Chiseled
  • 2,280
  • 8
  • 33
  • 59