2014-04-08 70 views
1

我试图匹配正则表达式和文本,但很难找到完全匹配。 这里是测试文本正则表达式检测换行符

SimulationControl, 
     \unique-object 
     \memo Note that the following 3 fields are related to the Sizing:Zone, Sizing:System, 
     \memo and Sizing:Plant objects. Having these fields set to Yes but no corresponding 
     \memo Sizing object will not cause the sizing to be done. However, having any of these 
     \memo fields set to No, the corresponding Sizing object is ignored. 
     \memo Note also, if you want to do system sizing, you must also do zone sizing in the same 
     \memo run or an error will result. 
    A1, \field Do Zone Sizing Calculation 
     \note If Yes, Zone sizing is accomplished from corresponding Sizing:Zone objects 
     \note and autosize fields. 
     \type choice 
     \key Yes 
     \key No 
     \default No 
    A2, \field Do System Sizing Calculation 
     \note If Yes, System sizing is accomplished from corresponding Sizing:System objects 
     \note and autosize fields. 
     \note If Yes, Zone sizing (previous field) must also be Yes. 
     \type choice 
     \key Yes 
     \key No 
     \default No 

Building, 
     \memo Describes parameters that are used during the simulation 
     \memo of the building. There are necessary correlations between the entries for 
     \memo this object and some entries in the Site:WeatherStation and 
     \memo Site:HeightVariation objects, specifically the Terrain field. 

所以我所试图做的是“建筑”上课前只选择文本。 这就是我做的正则表达式,但我不能检测空行前“大厦”,所以我可以前停下来,所以它选择所有文字

^[A-Z].*?,(\s*\\.*)*\s*[a-zA-Z0-9,\s\\:.\(\)\*;]*\n 

回答

1

如果你想要做的是选择一切,直到第一个空行,记住它只有两个连续的\n,对不对?您可以使用类似

^(?s).*?\n\n 

(?s)是一个内嵌的标志,这意味着.也将匹配换行符。 Demo为乐趣。


如果你想匹配任何这些块(不仅仅是第一个),你可以使用:

(?s).*?(?:\n\n|$) 

取下锚,(?:...)是一个非捕获组和\n\n|$会抓住空行或文档的结尾。

编辑

您可以使用\n\s*\n如果你担心有可能是在空行尾部空格,从@SebastianH

+0

这是我所期待的恭维,但我还有一个问题,该文件由几个像这样的块组成,那么我能做些什么来在文档中间选择一个块,例如,谢谢 – Helmi

+0

@Helmi:什么标准?请在下次尝试在原始问题中提供这些附加问题,这对每个人都很清楚。我编辑了答案。 – Robin

+0

在许多情况下,您不能确定“空行”完全没有空格(空格,制表符)。那么使用'\ n \ s * \ n'这样的模式代替'\ n \ n'可能会很好。 '\ s'匹配任何空格字符。 – SebastianH