Java 获取每个元素的完整组

Java 获取每个元素的完整组,java,regex,Java,Regex,我有一个很长的文本,里面充满了无用的元素,比如括号、空格、数字,我需要为我遇到的每一栋房子解析相同的值。我并不总是在每组中有相同数量的行。以下是我的部分文字: [04:04:04s] [startedRetrieving]getHouseName: house1 [04:04:04s] [startedRetrieving]random useless text [04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 120

我有一个很长的文本,里面充满了无用的元素,比如括号、空格、数字,我需要为我遇到的每一栋房子解析相同的值。我并不总是在每组中有相同数量的行。以下是我的部分文字:

[04:04:04s] [startedRetrieving]getHouseName: house1
[04:04:04s] [startedRetrieving]random useless text
[04:04:04s] [startedRetrieving]price (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]squaremtr (in doll) [min: 75, max:85]
[04:04:04s] [startedRetrieving]sellVal (in doll) [min: 1000, max: 1200]
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]random useless text
[04:04:05s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 200]
[04:06:04s] [startedRetrieving]getHouseName: house2
[04:06:04s] [startedRetrieving]price(in doll) [min: 1004, max 1100]
[04:06:04s] [startedRetrieving]squaremtr(in doll) [min: 85, max 99]
[04:06:04s] [startedRetrieving]sellVal(in doll) [min: 950, max: 1050]
[04:06:04s] [startedRetrieving]random useless text
[04:06:04s] [startedRetrieving]rentPrice(in doll) [min: 150, max: 290]
[04:09:04s] [startedRetrieving]getHouseName: house3
[04:09:04s] [startedRetrieving]price(in doll) [min: 1099, max: 1200]
[04:09:04s] [startedRetrieving]squaremtr(in doll) [min: 90, max: 110]
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]sellVal(in doll) [min: 1100, max: 1300]
[04:09:04s] [startedRetrieving]random useless text
[04:09:04s] [startedRetrieving]rentPrice(in doll) [min: 199, max: 300]
我想检索每个组的每个参数,如:

HouseName: house 1
MinPrice: 1000
MaxPrice: 1200
SquareMtrMin: 75
SquareMtrMax: 85
SellValMin: 1000
SellValMax: 1200
RentPriceMin: 150
RentPriceMax: 200

HouseName: house 2
MinPrice: 1004
MaxPrice: 1100
SquareMtrMin: 85
SquareMtrMax: 99
SellValMin: 950
SellValMax: 1050
RentPriceMin: 90
RentPriceMax: 110

HouseName: house 3
MinPrice: 1099
MaxPrice: 1200
SquareMtrMin: 90
SquareMtrMax: 110
SellValMin: 1100
SellValMax: 1300
RentPriceMin: 199
RentPriceMax: 300
这是我的密码:

String Name="";
String MinPrice = "";
String MaxPrice = "";
String SquareMtrMin = "";
String SquareMtrMax = "";
String SellValMin = "";
String SellValMax = "";
String RentPriceMin = "";
String RentPriceMax = "";

Pattern pattern = Pattern.compile("^getHouseName:\h+(.+)(?:\R(?!price \\(in doll\\) \\[).*)*\Rprice \\(in doll\\) \\[ (\d+)(?:\R(?!squaremtr \\(in doll\\) \\[).*)*\Rsquaremtr \\(in doll\\) \\[\h+(\d+)(?:\R(?!sellVal \\(in doll\\) \\[).*)*\RsellVal \\(in doll\\) \\[\h+(\d+)(?:\R(?!rentPrice \\(in doll\\) \\[).*)*\RrentPrice \\(in doll\\) \\[\h+(\d+)", Pattern.MULTILINE);
for(Matcher m = pattern.matcher(myText); m.find(){
System.out.println("Full match: " + m.group(0));
                for (int i = 1; i <= m.groupCount(); i++) {
                    System.out.println("Group " + i + ": " + m.group(i));
                }
}
String Name=”“;
字符串MinPrice=“”;
字符串MaxPrice=“”;
字符串SquareMtrMin=“”;
字符串SquareMtrMax=“”;
字符串SellValMin=“”;
字符串SellValMax=“”;
字符串RentPriceMin=“”;
字符串RentPriceMax=“”;
Pattern Pattern=Pattern.compile(“^getHouseName:\h+(.+)(?:\R(?!price\\(在玩偶中)\\[).*)\ \Rprice\\(在玩偶中)\[(\R(?!squaremtr\\\(在玩偶中)\\[)\(\R(?!sellVal\\(在玩偶中)\\\\[)*)\\[\h+(\d:(\R(?)在玩偶中)\\\\\\\\\\\\\(在玩偶中)\\\\\\\\\\\[\R+)\(在玩偶中)\\\\\\\\\\\\\[\R+)\\\\\\\\\\\\\\\\\\\\\\[\R+\R+\“,模式。多行);
for(Matcher m=pattern.Matcher(myText);m.find(){
System.out.println(“完全匹配:+m.group(0));

对于(inti=1;i,您可以扩展模式以查找字符串下面的值,并使用组值作为变量

在该部分中,您还必须匹配
\[\h+(\d+)
min:
部分。如果要使用
\R
的负前瞻扫描下一行,首先必须匹配到当前行的末尾

^.*\bgetHouseName:\h+(.+)(?:\R(?!.*price).*)*\R.*price\h*\(in doll\)\h+\[min:\h+(\d+),\h+max:?\h+(\d+)\](?:\R(?!.*squaremtr).*)*\R.*squaremtr\h*\(in doll\)\h+\[min:?\h*(\d+),\h+max:?\h*(\d+)\](?:\R(?!.*sellVal).*)*\R.*sellVal\h*\(in doll\)\h+\[min:?\h*(\d+),\h+max:?\h*(\d+)\](?:\R(?!.*rentPrice).*)*\R.*rentPrice\(in doll\)\h+\[min:?\h*(\d+),\h*max:?\h*(\d+)\]

|

字符串不以
^getHouseName
开头,例如
price
也不在字符串开头。即使删除^I也不能有任何结果,例如,可以这样开头
^.*\bMethouseName:\h+(.+)(?:\R(?。*price.*)*\R.*price\h+\(在玩偶中)\h+\[min:\h+(\d+,\h+max:\h+(\d+)](?!*squaremtr.*)*\R.*squaremtr\h+\(在玩偶\)中)\h+\[min:(\d+)
如果你对所有的值都这样做,你可以使用捕获组。这不起作用,我没有打印任何东西。我已经添加了一个java演示如何使用组值。谢谢!这对第一所房子有效,但我如何访问其他房子?它在第一所房子的末尾停止parameters@thranduil90我的错,我看到了有一些可选空格和
我已经更新了正则表达式和演示。很酷,非常感谢!我现在正在根据我的代码调整它。如果我的值Min和Max的形式是12.098765呢?我如何修改正则表达式?然后可以使用
\d+
而不是
\d+(?:\。\d+)?
和Java中的双转义反斜杠,如
\\d+(?:\\.\d+)