简体   繁体   English

提取正则表达式Java的一部分

[英]Extract a part of regular expression java

I need help to know how to extract just a part of regular expression in Java with reference. 我需要帮助来了解如何通过引用提取Java中正则表达式的一部分。 Example: I have a text like: 示例:我有一个类似的文本:

"Jean Moulin - Porte d'Orléans - Paris 14e Jean Moulin. Apartment 37 m² renovated in 2013. Materials and quality services. Large living room with kitchen, separate bedroom area, dressing room, toilet / sink, large shower, toilet with sink and washing machine equipment, marble floor, double glazed window sound. Quiet, functional, spacious. On the first floor with open views, elevator, guards. Furniture and equipment new appliances. € 310,000. “ Jean Moulin-奥尔良门-巴黎14e Jean Moulin。公寓37平方米,于2013年进行了翻新。材料和优质的服务。带厨房的大客厅,独立的卧室区,更衣室,卫生间/洗手池,大淋浴间,带洗手池的洗手间和洗衣机设备,大理石地板,双层玻璃窗声音,安静,功能齐全,宽敞,位于一楼,可欣赏风景,设有电梯,护栏,家具和设备新器具,310,000欧元。

Rooms: 2 Bedrooms: 1 Surface: 37 m² " 客房:2卧室:1面积:37m²“

And I want only to take the number of rooms with the mark "rooms" How I can do? 我只想取带有“ rooms”标记的房间数,怎么办? I know just that: 我只知道:

Pattern p;
Matcher m;
p= Pattern.compile("Rooms: +[0-9]");

but I want only the number, I want to delete "Rooms: " from the result. 但是我只想要数字,我想从结果中删除“ Rooms:”。

And the 2nd question, How to take the name of the city with a regular expression, because if I have a lot of text with different name of city: like Paris, London, Lyon, Rome etc... How I can do to make one regular expression that does this? 第二个问题是,如何使用正则表达式来命名城市名称,因为如果我有很多文字使用不同的城市名称:例如巴黎,伦敦,里昂,罗马等,我该如何做一个正则表达式可以吗?

I want only the number, I want to delete "Rooms: " from the result. 我只想要数字,我想从结果中删除“ Rooms:”。

Use Matcher#group() to get the matched group that is enclosed inside the Parenthesis () that is used to enable grouping of regex phrases. 使用匹配器#组()来获取被封闭在括号内的匹配组()用来实现正则表达式的短语进行分组。

Here group(1) returns the (\\\\d+) if found. 如果找到, group(1)将返回(\\\\d+)

Sample code: 样例代码:

    Pattern pattern = Pattern.compile("Rooms:\\s+(\\d+)");
    Matcher matcher = pattern.matcher("Rooms: 2 Bedrooms: 1 Surface: 37 m² ");

    if (matcher.find()) {
        System.out.println(matcher.group(1)); // output 2 only
    }

Please have a look at Java Regex Pattern where all the pattern are explained in details. 请查看Java Regex模式 ,其中详细说明了所有模式。 Find sample code here on Java Tutorials - Quantifiers Java教程-量词上找到示例代码

使用带有反向引用的replaceAll()在一行中提取它:

String rooms = input.replaceAll("(?is).*\\brooms\\s+(\\d+).*", "$1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM