如何解析regex中的单行和多行



我需要解析出单行数据和多行数据。如何仅使用一个正则表达式来完成此操作?

-------------------------------------------
/ Vegetation /
-------------------------------------------
-- 
-- Shrubs [-] = Number of shrubs
-- Shrub_x [m/rad] = The position of shrub nr x in either (x,y,z) or in (latitude, longitude, h)
--          (XY/WGS84, height above msl)
-- 
------------------------------------------
I.Shrubs := 3; -- {MIN:0 MAX:10}
Car.Shrub_1 := (0.0, 0.0, 1.5);
Car.Shrub_2 := (3.4, 10.0, 0.2);
Car.Shrub_3 := (5.0, 5.0, 2.0);
-------------------------------------------
/ Lawn /
-------------------------------------------
-- Some general info thats not required but that could be written here.
-- 
-- Lawn_Attitude [rad] = The attitude of the lawn as Euler angles. 
--                North, East and Up is used as reference frame.
-- Lawn_Quality [-] = The quality index of the lawn.
--
-------------------------------------------
Eul.Lawn_Attitude := (0.7853981,-0.23651236, 0.017); 
I.Lawn_Quality := 5; --{0,1,2,3,4,5}

现在我使用的是:

-- w+ [.+n(?!--[ t]{2,}) # for single line and
-- [^ tn].+n--s{3,}.+ # for multi line.

我需要考虑的是,数据可能同时使用制表符和空格编写,因为用户可能使用其中之一,并且一些IDE认为制表符是4个空格。

您可以匹配--,后跟一个空格,然后是一个非空白字符和行的其余部分。

然后匹配以下所有以--和至少3个空白字符开头的行。

注意,s也可以匹配换行符。

^-- S.*(?:n--s{3}.*)*

Regex演示

如果两行的开头都必须有一个左方括号,并且您希望匹配3个没有换行符的空白字符:

^-- [^][n]*[.*(?:n--[^Sn]{3}.*)*

Regex演示

最新更新