【问题标题】:Regex how to get custom section text正则表达式如何获取自定义部分文本
【发布时间】:2014-03-12 06:27:38
【问题描述】:

我编写了一个需要解析器来解析文件的 .Net 应用程序,其中包含如下部分:

[Params]
testParam = false
[Data]
testdata
[Info]
//sominfo

如您所见,此文件包含 3 个部分。部分标题可以不同,例如,这里是 [{Text}],也可以是 ${Text}。我想给用户一个指定标题模式的可能性。那么是否有一个正则表达式可以提取部分文本,例如对于部分 [Params] 文本是 testParam = false,对于部分 [Data] 文本是 testdata 等。

我的正则表达式不是很好,如果有任何帮助,我将不胜感激

【问题讨论】:

    标签: .net regex parsing


    【解决方案1】:

    要解析特定值,可以说Params

    \[Params][\s\n\r]+([^\[]*)
    

    解析所有值:

    \[[^\]]+\][\s\n\r]+([^\[]*)
    

    【讨论】:

      【解决方案2】:

      这似乎是一个INI 文件。是吗 ?试着用这个tutorial去读写INI文件。

      如果你真的想用正则表达式试试这个:

      (?:\[(?<section>.+)\]\s+(?<!\[)(?<content>.+)(?!\]))
      

      Demo

      【讨论】:

        【解决方案3】:

        使用此代码读取和更改ini文件

        Imports System.IO
        Imports System.Text.RegularExpressions
        Imports System.Collections
        Imports System.Diagnostics
        
        Public Class IniFile
            Private m_sections As Hashtable
        
            Public Sub New()
                m_sections = New Hashtable(StringComparer.InvariantCultureIgnoreCase)
            End Sub
        
            Public Sub Load(ByVal sFileName As String)
                Dim tempsection As IniSection = Nothing
                Dim oReader As New StreamReader(sFileName)
                Dim regexcomment As New Regex("^([\s]*#.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
                Dim regexsection As New Regex("^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
                Dim regexkey As New Regex("^\s*([^=\s]*)[^=]*=(.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
                While Not oReader.EndOfStream
                    Dim line As String = oReader.ReadLine()
                    If line <> String.Empty Then
                        Dim m As Match = Nothing
                        If regexcomment.Match(line).Success Then
                            m = regexcomment.Match(line)
                        ElseIf regexsection.Match(line).Success Then
                            m = regexsection.Match(line)
                            tempsection = AddSection(m.Groups(1).Value)
                        ElseIf regexkey.Match(line).Success AndAlso tempsection IsNot Nothing Then
                            m = regexkey.Match(line)
                            tempsection.AddKey(m.Groups(1).Value).Value = m.Groups(2).Value
                        ElseIf tempsection IsNot Nothing Then
                            tempsection.AddKey(line)
                        Else
                        End If
                    End If
                End While
                oReader.Close()
            End Sub
        
            Public Sub Save(ByVal sFileName As String)
                Dim oWriter As New StreamWriter(sFileName, False)
                For Each s As IniSection In Sections
                    oWriter.WriteLine(String.Format("[{0}]", s.Name))
                    For Each k As IniSection.IniKey In s.Keys
                        If k.Value <> String.Empty Then
                            oWriter.WriteLine(String.Format("{0}={1}", k.Name, k.Value))
                        Else
                            oWriter.WriteLine(String.Format("{0}", k.Name))
                        End If
                    Next
                Next
                oWriter.Close()
            End Sub
        
            Public ReadOnly Property Sections() As System.Collections.ICollection
                Get
                    Return m_sections.Values
                End Get
            End Property
        
            Public Function AddSection(ByVal sSection As String) As IniSection
                Dim s As IniSection = Nothing
                sSection = sSection.Trim()
                If m_sections.ContainsKey(sSection) Then
                    s = DirectCast(m_sections(sSection), IniSection)
                Else
                    s = New IniSection(Me, sSection)
                    m_sections(sSection) = s
                End If
                Return s
            End Function
        
            Public Function GetSection(ByVal sSection As String) As IniSection
                sSection = sSection.Trim()
                If m_sections.ContainsKey(sSection) Then
                    Return DirectCast(m_sections(sSection), IniSection)
                End If
                Return Nothing
            End Function
        
            Public Function GetKeyValue(ByVal sSection As String, ByVal sKey As String) As String
                Dim s As IniSection = GetSection(sSection)
                If s IsNot Nothing Then
                    Dim k As IniSection.IniKey = s.GetKey(sKey)
                    If k IsNot Nothing Then
                        Return k.Value
                    End If
                End If
                Return String.Empty
            End Function
        
            Public Function SetKeyValue(ByVal sSection As String, ByVal sKey As String, ByVal sValue As String) As Boolean
                Dim s As IniSection = AddSection(sSection)
                If s IsNot Nothing Then
                    Dim k As IniSection.IniKey = s.AddKey(sKey)
                    If k IsNot Nothing Then
                        k.Value = sValue
                        Return True
                    End If
                End If
                Return False
            End Function
        
            Public Class IniSection
                Private m_pIniFile As IniFile
                Private m_sSection As String
                Private m_keys As Hashtable
        
                Protected Friend Sub New(ByVal parent As IniFile, ByVal sSection As String)
                    m_pIniFile = parent
                    m_sSection = sSection
                    m_keys = New Hashtable(StringComparer.InvariantCultureIgnoreCase)
                End Sub
        
                Public ReadOnly Property Keys() As System.Collections.ICollection
                    Get
                        Return m_keys.Values
                    End Get
                End Property
        
                Public ReadOnly Property Name() As String
                    Get
                        Return m_sSection
                    End Get
                End Property
        
                Public Function AddKey(ByVal sKey As String) As IniKey
                    sKey = sKey.Trim()
                    Dim k As IniSection.IniKey = Nothing
                    If sKey.Length <> 0 Then
                        If m_keys.ContainsKey(sKey) Then
                            k = DirectCast(m_keys(sKey), IniKey)
                        Else
                            k = New IniSection.IniKey(Me, sKey)
                            m_keys(sKey) = k
                        End If
                    End If
                    Return k
                End Function
        
                Public Function GetKey(ByVal sKey As String) As IniKey
                    sKey = sKey.Trim()
                    If m_keys.ContainsKey(sKey) Then
                        Return DirectCast(m_keys(sKey), IniKey)
                    End If
                    Return Nothing
                End Function
        
                Public Class IniKey
                    Private m_sKey As String
                    Private m_sValue As String
                    Private m_section As IniSection
                    Protected Friend Sub New(ByVal parent As IniSection, ByVal sKey As String)
                        m_section = parent
                        m_sKey = sKey
                    End Sub
                    Public ReadOnly Property Name() As String
                        Get
                            Return m_sKey
                        End Get
                    End Property
        
                    Public Property Value() As String
                        Get
                            Return m_sValue
                        End Get
                        Set(ByVal value As String)
                            m_sValue = value
                        End Set
                    End Property
        
                End Class
            End Class
        End Class
        

        【讨论】:

          猜你喜欢
          • 2012-02-11
          • 1970-01-01
          • 2014-09-04
          • 1970-01-01
          • 2016-12-27
          • 2019-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多