C# 正则表达式-匹配特定字符(多次)并忽略注释
我不是正则表达式的专家,需要一些帮助来建立一个正则表达式 我使用的是Powershell及其[regex]类型,这是一个,最终目标是读取一个toml文件(底部的示例数据,或使用此文件),其中我需要:C# 正则表达式-匹配特定字符(多次)并忽略注释,c#,regex,C#,Regex,我不是正则表达式的专家,需要一些帮助来建立一个正则表达式 我使用的是Powershell及其[regex]类型,这是一个,最终目标是读取一个toml文件(底部的示例数据,或使用此文件),其中我需要: 匹配一些值(在“\uuuuuuuuuuuuuuuuuuuuuux”之间的值) 忽略评论。(注释以“#”)开头) 要匹配这些值并将它们放入捕获组,以下正则表达式起作用: match the template value (values between "__" ): __(?<tokenNa
- 匹配一些值(在“\uuuuuuuuuuuuuuuuuuuuuux”之间的值)
- 忽略评论。(注释以“#”)开头)
match the template value (values between "__" ):
__(?<tokenName>[\w\.]+)__
当我把它们放在一起时,问题就开始了
^(?!\s*\t*#).*__(?<tokenName>[\w\.]+)__
我认为更简单的解决方案是匹配给定的字符串,前提是在同一行的前面没有“#”。
可能吗
编辑:
@thefourth bird提出的第一个表达式非常有效,只需指定多行修饰符即可。
在PowerShell中,最终(可运行)结果如下所示
[regex]$reg = "(?m)(?<!^.*#.*)__(?<tokenName>[\w.]+)__"
$text = '
#templateFile
[Agent]
Prop1 = "__Data.Agent.Prop1__"
Prop2 = [__Data.Agent.Prop2__]
Prop5 = ["__Data.Agent.Prop5a__","__Data.Agent.Prop5b__"]
#a comment
#Prop3 = "__Data.Agent.Prop3__"
Prop4 = [__Data.Agent.Prop4__] #sample usage comment __Data.Agent.xxx__
'
$reg.Matches($text) | Format-Table
#This returns
Groups Success Name Captures Index Length Value
------ ------- ---- -------- ----- ------ -----
{0, tokenName} True 0 {0} 31 20 __Data.Agent.Prop1__
{0, tokenName} True 0 {0} 62 20 __Data.Agent.Prop2__
{0, tokenName} True 0 {0} 94 21 __Data.Agent.Prop5a__
{0, tokenName} True 0 {0} 118 21 __Data.Agent.Prop5b__
{0, tokenName} True 0 {0} 194 20 __Data.Agent.Prop4__
[regex]$reg=“(?m)(?[\w.]+)\uuuux”
$text='1
#模板文件
[代理人]
Prop1=“_数据。代理。Prop1__”
Prop2=[[uuuuu Data.Agent.Prop2]
Prop5=[“\uuuu数据.Agent.Prop5a\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
#评论
#Prop3=“\uuuu数据。代理。Prop3\uuuuu”
Prop4=[\uuuu Data.Agent.Prop4\uuu]#示例用法注释uuu Data.Agent.xxx__
'
$reg.Matches($text)|格式表
#这是回报
组成功名称捕获索引长度值
------ ------- ---- -------- ----- ------ -----
{0,tokenName}True 0{0}31 20__Data.Agent.Prop1__
{0,tokenName}True 0{0}62 20__Data.Agent.Prop2__
{0,tokenName}True 0{0}94 21__Data.Agent.Prop5a__
{0,tokenName}True 0{0}118 21__Data.Agent.Prop5b__
{0,tokenName}True 0{0}194 20__Data.Agent.Prop4__
我认为您可以利用检查前面的内容是否不包含
来解释Prop4中的注释
(?<!^.*#.*)__(?<tokenName>[\w.]+)__
(?[\w.]+)__
如果Prop4应该有2个匹配项,则可以使用:
(?<!^[ \t]*#.*)__(?<tokenName>[\w.]+)__
(?[\w.]+)__
这两个表达式都需要多行修改器才能正常工作。
可以通过在开头添加(?m)内联指定。(或在支持它的构造函数中指定)
(?m)(?[\w.]+)__
[regex]$reg = "(?m)(?<!^.*#.*)__(?<tokenName>[\w.]+)__"
$text = '
#templateFile
[Agent]
Prop1 = "__Data.Agent.Prop1__"
Prop2 = [__Data.Agent.Prop2__]
Prop5 = ["__Data.Agent.Prop5a__","__Data.Agent.Prop5b__"]
#a comment
#Prop3 = "__Data.Agent.Prop3__"
Prop4 = [__Data.Agent.Prop4__] #sample usage comment __Data.Agent.xxx__
'
$reg.Matches($text) | Format-Table
#This returns
Groups Success Name Captures Index Length Value
------ ------- ---- -------- ----- ------ -----
{0, tokenName} True 0 {0} 31 20 __Data.Agent.Prop1__
{0, tokenName} True 0 {0} 62 20 __Data.Agent.Prop2__
{0, tokenName} True 0 {0} 94 21 __Data.Agent.Prop5a__
{0, tokenName} True 0 {0} 118 21 __Data.Agent.Prop5b__
{0, tokenName} True 0 {0} 194 20 __Data.Agent.Prop4__
(?<!^.*#.*)__(?<tokenName>[\w.]+)__
(?<!^[ \t]*#.*)__(?<tokenName>[\w.]+)__
(?m)(?<!^.*#.*)__(?<tokenName>[\w.]+)__