I am trying to extract data from this String:
Hello there. Blah blahblah blah Building 016814 - Door 01002 BlahBLAHblah DUHHH 78787 blah, Blah blah Building Dr 4647 8989 BLAHBlah blah blahBlah
I am looking to loop through the String and pull each instance of Building and Door number and output to console. However, since both instances of Building and Door number are different form one another, I know that I will need to use two different Regex patterns.
Here is my code:
public static void main(String agrs[]) {
String myStr = "Hello there. Blah blahblah blah Building 016814 - Door 01002"+
" BlahBLAHblah DUHHH 78787 blah, Blah blah Building Dr 4647 8989 BLAHBlah blah blahBlah";
Pattern p = Pattern.compile("Building.+?(?:[Dd]).+?(\\d+).+?(\\d+)");
Pattern p1 = Pattern.compile("Building.+?(\\d+).+?(?:[Dd]).+?(\\d+)");
Matcher m = p.matcher(myStr);
Matcher m1 = p1.matcher(myStr);
while(m1.find() && m.find()) {
System.out.print(" Building " + m1.group(1) + " " + "Door ");
System.out.print(m1.group(2));
System.out.print(" Building " + m.group(1)+" "+ "Door "+m.group(2));
}
And here is my output:
Building 016814 Door 01002 Building 01002 Door 78787
I know it has something to do with my p regex pattern. It seems to be pulling any numbers in between. I am a newbie to regex so let me know if you need more info about this. Any help will be much appreciated.
Building Dr 4647 8989, should it match anything? I took that to mean it should matchDr 4647.