C# Regex.Split

本文关键字:Split Regex | 更新日期: 2023-09-27 17:57:10

我在复杂字符串中有一个特殊的问题:

字符串模式:

     Description,Title,ContentFooterLeft[JE_Boxes: Identifier,Box,Namespace,Title,Channels,PriorityChannels,ForceChannel,SingleQuery,Template,Elements[JE_Example: Lead],LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],],],Keywords

树模式:

Description,
Title,
ContentFooterLeft
    [
        JE_Boxes: 
                Identifier,
                Box,
                Namespace,
                Title,
                Channels,
                PriorityChannels,
                ForceChannel,
                SingleQuery,
                Template,
                Elements
                    [
                        JE_Example:
                                Lead
                    ],
                LastNews
                    [
                        JE_Article: 
                                Lead,
                                Galeria
                                    [
                                        JE_Gallery:
                                                Title,
                                                Banner 
                                                    [
                                                        JE_Banner: 
                                                                Title
                                                    ],
                                    ],
                    ],
    ],
Keywords

我与Regex.Split的目标如下:

1º [步进] 调用功能:

Regex.Split("Description,Title,ContentFooterLeft[JE_Boxes: Identifier,Box,Namespace,Title,Channels,PriorityChannels,ForceChannel,SingleQuery,Template,Elements[JE_Example: Lead],LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],],],Keywords", "Regex expression")

1º 结果:

Description
Title
ContentFooterLeft[JE_Boxes:Identifier,Box,Namespace,Title,Channels,PriorityChannels,ForceChannel,SingleQuery,Template,Elements[JE_Example: Lead],LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],],]
Keywords

2º [步骤] 调用功能:

Regex.Split("ContentFooterLeft[JE_Boxes:Identifier,Box,Namespace,Title,Channels,PriorityChannels,ForceChannel,SingleQuery,Template,Elements[JE_Example: Lead],LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],],]", "Regex expression")

2º 结果:

JE_Boxes
Identifier
Box
Namespace
Title
Channels
PriorityChannels
ForceChannel
SingleQuery
Template
Elements[JE_Example: Lead]
LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],]

3º [步进] 调用功能:

Regex.Split("Elements[JE_Example: Lead]", "Regex expression")
Regex.Split("LastNews[JE_Article: Lead, Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],],]", "Regex expression")

3º 结果:

JE_Example
Lead

JE_Article
Lead
Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],]

4º [步进] 调用功能:

Regex.Split("Galeria [JE_Gallery: Title,Banner [JE_Banner: Title],]", "Regex expression")

4º 结果:

JE_Gallery
Title
Banner [JE_Banner: Title]

5º [步进] 调用功能:

Regex.Split("Banner [JE_Banner: Title]", "Regex expression")

5º 结果:

JE_Banner
Title

很明显,当我知道正则表达式时,我会以递归方法做到这一点......对不起,我的长文,但这是呈现我的问题的最佳方式......

C# Regex.Split

我认为在这种情况下

,编写解析器实际上可能比编写正则表达式更容易。您可以使用Irony,这是一个优秀且易于使用的库。