I have a huge txt that can contain several house names and, for each house, there are some values specific to that specific house, and so on. Here is a similar part of my txt:
getHouseName: house1
random useless text
price: 1000
squaremtr: 75
sellVal: 1000
random useless text
random useless text
random useless text
rentPrice: 150
getHouseName: house2
price: 1004
squaremtr: 85
sellVal: 950
random useless text
rentPrice: 150
getHouseName: house3
price: 1099
squaremtr: 90
random useless text
random useless text
sellVal: 1100
random useless text
rentPrice: 199
I would like, for every house, to retrieve values specific for each house and store them into a variable using regexes. Right now this is my code:
public void testHouse() {
Scanner txt = new Scanner(new File("path//to//file"));
String houseName ="";
String price = "";
String squaremtr = "";
String sellVal = "";
String rentPrice = "";
Pattern houseNamePatt = Pattern.compile("getHouseName: ((_!getHouseName: \\s).)*", Pattern.DOTALL);
while(txt.hasNextLine()) {
String str = txt.nextLine();
Matcher m = houseNamePatt.matcher(str);
if(m.find) {
houseName=str.substring(m.end());
System.out.println("houses: " + m.group());
}
}
}
But in this case I'm just getting a list with all the house names, not the lines between each name and I definitely can't assign the values of a specific house to my variables. Where am I wrong? Thank you
getHouseName:\h+(.*)\Rprice:\h+(\d+)etc..