简体   繁体   English

帮助定义RegEx以从INI文件捕获行定义

[英]Help Defining RegEx to Capture Line Definition from INI file

Need help defining an expression to capture the four numerics per line that define the lines on a map. 需要帮助定义一个表达式以捕获每条线的四个数字,这些数字定义了地图上的线。

59.684 -4.251 59.575 -5.576
59.575 -5.576 59.437 -6.899
59.437 -6.899 59.27 -8.218
-7.346 23.196 -7.409 23.233
-7.409 23.233 -7.46 23.285
-7.46 23.285 -7.495 23.349
-7.495 23.349 -7.51 23.42
9.172 39.362 9.134 39
9.134 39.288 9.087 39.219
9.087 39.219 9.032 39.155
9.032 39.155 8.97 39.099
8.97 39.099 8.901 39

Thanks, 谢谢,

Dave 戴夫

(?<column1>\S+) (?<column2>\S+) (?<column3>\S+) (?<column4>\S+)

You can run that expression on every line and it will extract the values between spaces into four named capturing groups. 您可以在每一行上运行该表达式,它将将空格之间的值提取到四个命名的捕获组中。

The <columnN> parts are the names of the groups you're capturing. <columnN>部分是您要捕获的组的名称。 You refer to them as column1 , column2 , etc. 您将它们称为column1column2等。

You don't need regular expressions for that. 您不需要为此的正则表达式。 Untested: 未经测试:

Using sr As New StreamReader("myFile.txt")
    Do
        Dim line = sr.ReadLine()
        If line Is Nothing Then Exit Do

        Dim values = line.Split()
        Dim x1 = Double.Parse(values(0), CultureInfo.InvariantCulture)
        Dim y1 = Double.Parse(values(1), CultureInfo.InvariantCulture)
        Dim x2 = Double.Parse(values(2), CultureInfo.InvariantCulture)
        Dim y2 = Double.Parse(values(3), CultureInfo.InvariantCulture)

        ' do something with these values '

    Loop
End Using

regex.matches与字符串"(\\S*[\\d\\.]+)"迭代返回的MatchColection ,该值将具有您想要的字符串。

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

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