.net Regex如何获取自定义节文本
我编写了一个需要解析器来解析文件的.Net应用程序,其中包含以下部分:.net Regex如何获取自定义节文本,.net,regex,parsing,.net,Regex,Parsing,我编写了一个需要解析器来解析文件的.Net应用程序,其中包含以下部分: [Params] testParam = false [Data] testdata [Info] //sominfo 如您所见,该文件包含3个部分。节头可以不同,例如,在这里它是[{Text}],也可以是${Text}。我想给用户一个可能的指定头模式。所以有一个正则表达式可以提取节的文本,例如,对于节[Params],text是testParam=false,对于节[Data]text是testdata等等 我对正则表达
[Params]
testParam = false
[Data]
testdata
[Info]
//sominfo
如您所见,该文件包含3个部分。节头可以不同,例如,在这里它是[{Text}],也可以是${Text}。我想给用户一个可能的指定头模式。所以有一个正则表达式可以提取节的文本,例如,对于节[Params],text是testParam=false,对于节[Data]text是testdata等等
我对正则表达式不是很在行,如果有任何帮助,我将非常感激。这似乎是一个
INI
文件。它是?尝试使用此选项读取和写入INI文件
如果您真的想使用正则表达式,请尝试一下:
(?:\[(?<section>.+)\]\s+(?<!\[)(?<content>.+)(?!\]))
(?:\[(?。+)\]\s+(?。+)(?!\]))
要解析特定值,可以说
Params
:
\[Params][\s\n\r]+([^\[]*)
要分析所有值,请执行以下操作:
\[[^\]]+\][\s\n\r]+([^\[]*)
使用此代码读取和更改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
Imports System.IO
导入System.Text.RegularExpressions
导入系统集合
导入系统。诊断
公共类文件
私有m_节作为哈希表
公共分新()
m_sections=新哈希表(StringComparer.InvariantCultureInogoreCase)
端接头
公共子加载(ByVal sFileName作为字符串)
Dim tempsection As IniSection=无
Dim oReader作为新的StreamReader(sFileName)
Dim regexcomment作为新正则表达式(“^([\s]*#.*”,(RegexOptions.Singleline或RegexOptions.IgnoreCase))
将regexsection作为新的Regex(“^[\s]*\[\s]*([^\[\s].[^\s\]])[\s]*\[\s]*$”,(RegexOptions.Singleline或RegexOptions.IgnoreCase))
将regexkey设置为新的Regex(“^\s*([^=\s]*)[^=]*=(.*”,(RegexOptions.Singleline或RegexOptions.IgnoreCase))
而不是oReader.EndOfStream
以字符串形式标注行=oReader.ReadLine()
如果是行字符串。则为空
将m变暗为匹配=无
如果regexcomment.Match(line).Success则
m=regexcomment.Match(行)
ElseIf regexsection.Match(line)。然后成功
m=regexsection.Match(行)
tempsection=AddSection(m.Groups(1).Value)
那么,ElseIf regexkey.Match(line.Success)和also temporation就不是什么了
m=regexkey.Match(行)
tempsection.AddKey(m.Groups(1).Value=m.Groups(2).Value
否则我就什么都不是了
tempsection.AddKey(行)
其他的
如果结束
如果结束
结束时
oReader.Close()
端接头
公共子存储(ByVal sFileName作为字符串)
Dim oWriter作为新的StreamWriter(sFileName,False)
对于每个部分,请按部分中的部分
WriteLine(String.Format(“[{0}]”,s.Name))
对于s.键中的每个k As INICATION.IniKey
如果k.值字符串为空,则
WriteLine(String.Format(“{0}={1}”,k.Name,k.Value))
其他的
WriteLine(String.Format(“{0}”,k.Name))
如果结束
下一个
下一个
oWriter.Close()
端接头
公共只读属性节()作为System.Collections.ICollection
得到
返回m_.Values
结束
端属性
公共函数AddSection(ByVal sSection作为字符串)作为InSection
尺寸s为INI截面=无
sSection=sSection.Trim()
如果m_sections.ContainsKey(sSection),则
s=DirectCast(m_区段(s区段)、INI区段)
其他的
s=新截面(Me,s截面)
m_截面(s截面)=s
如果结束
返回s
端函数
公共函数GetSection(ByVal sSection作为字符串)作为InSection
sSection=sSection.Trim()
如果m_sections.ContainsKey(sSection),则
返回DirectCast(m_区段(SSE区段),INI区段)
如果结束
一无所获
端函数
公共函数GetKeyValue(ByVal sSection作为字符串,ByVal sKey作为字符串)作为字符串
尺寸s作为IniSection=GetSection(sSection)
如果s不是什么,那么
尺寸k为IniSection.IniKey=s.GetKey(sKey)
如果k不是什么,那么
返回k值
如果结束
如果结束
返回字符串。空
端函数
作为布尔值的公共函数SetKeyValue(ByVal sSection作为字符串,ByVal sKey作为字符串,ByVal sValue作为字符串)
尺寸s作为IniSection=添加节(sSection)
如果s不是什么,那么
尺寸k为IniSection.IniKey=s.AddKey(sKey)
如果k不是什么,那么
k、 值=S值
返回真值
如果结束
如果结束
返回错误
端函数
公共课组
私有m_pIniFile作为INI文件
私有m_section作为字符串
私有m_密钥作为哈希表
受保护的好友子新建(ByVal父项作为InFile,ByVal分区作为字符串)
m_pIniFile=父级
m_sSection=sSection
m_keys=新哈希表(StringComparer.InvariantCultureInogoreCase)
端接头
公共只读属性键()作为System.Collections.ICollection
得到
返回m_keys.Values
结束
端属性
作为字符串的公共只读属性名()
得到
返回m_选择
结束
端属性
公共函数AddKey(ByVal sKey作为字符串)作为IniKey
sKey=sKey.Trim()
尺寸k为INICEATION.IniKey=无
如果sKey.Length为0,则
如果m_keys.ContainsKey(sKey)那么
k=DirectCast(m_键(sKey)、IniKey)
其他的
k=新的INICEACTION.IniKey(Me,sKey)
m_键(sKey)=k
如果结束
如果结束
返回k
端函数
公开的