匹配特定单词周围的单词 - 正则表达式

时间:2015-01-28 15:47:42

标签: regex

说我有这些字符串:

1. TOP FLAT
2. FLAT 7
3. FLAT B
4. GROUND FLOOR FLAT
5. STUDIO 1.02
6. APARTMENT 10
7. GROUND AND FIRST FLOOR MAISONETTE
8. FLAT A

正则表达式会给我什么:

1. TOP
2. 7
3. B
4. GROUND FLOOR
5. 1.02 
6. 10
7. Ground and first floor
8. A

区分大小写并不重要。在过去的几个小时里,我一直在反对这一点,但我似乎无处可去。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

(这是perl,但RE工作)

while ( <DATA> ) {
    s/(house|maisonette|studio|apartment|flat) ?//gi; 
    print;
}

 __DATA__
1. TOP FLAT
2. FLAT 7
3. FLAT B
4. GROUND FLOOR FLAT
5. STUDIO 1.02
6. APARTMENT 10
7. GROUND AND FIRST FLOOR MAISONETTE
8. FLAT A

给出:

1. TOP 
2. 7
3. B
4. GROUND FLOOR 
5. 1.02
6. 10
7. GROUND AND FIRST FLOOR 
8. A

答案 1 :(得分:0)

[ ]*(?:flat|studio|apartment|Maisonette)[ ]*

试试这个。space。见。演示。

https://regex101.com/r/pM9yO9/10