开发者

Regex nested groups

I am attempting to extract all data from a file with one regex expression. Since there are optional(?,*) and repitious(*,+) expressions, it would be difficult to enumerate through the captures, at least in a readable and understandable fashion. Therefore, I am using named groups. However the data is in an XML like structure; neseted elements can have complex elements. So I am using nested name groups. However, one I have captured a group, how do I retrieve the nested groups by name?

Dim mtch = Sys开发者_如何学运维tem.Text.RegularExpressions.Regex.Match(fullText, _
                                       "\s+(?<PolyID>\d+)" & _
                                       "\s+(?<Center>" & _
                                            "(?<LAT>\-?\d\.\d+E\+\d{2})" & _
                                            "\s+(?<LON>\-?\d\.\d+E\+\d{2})" & _
                                        ")\n(?<Point>\s+" & _
                                            "(?<LAT>\-?\d\.\d+E\+\d{2})" & _
                                            "\s+(?<LON>\-?\d\.\d+E\+\d{2})" & _
                                        "\n)+END\n", _
                                        System.Text.RegularExpressions.RegexOptions.Singleline)

I am trying to get the

mtch.Group("Center").Group("LAT").Value

How can I retrieve named subgroups of the current group?


I would suggest that you rename your groups to "CenterLAT", "CenterLON", "PointLAT", and "PointLON". You might be able to get what you want from the mtch.Groups("Center").Captures collection, but you can't index that collection by name.


Instead of using Regex, have you considered using XSLT? or XLinq?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜