gpt4 book ai didi

c# - 我怎样才能在 C# 中获得这个正则表达式?

转载 作者:太空宇宙 更新时间:2023-11-03 12:32:52 24 4
gpt4 key购买 nike

我正在尝试匹配其中包含 type:"Data" 的任何 block ,然后将其替换为我想要的文本。
下面给出了一个示例输入,可以有一个或多个:

layer {
name: "cifar"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mean_file: "examples/cifar10/mean.binaryproto"
mirror: true
#crop_size: 20
}

# this is a comment!
data_param {
source: "examples/cifar10/cifar10_train_lmdb"
batch_size: 100
backend: LMDB
}
}
layer {
name: "cifar"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mean_file: "examples/cifar10/mean.binaryproto"
}
data_param {
source: "examples/cifar10/cifar10_test_lmdb"
batch_size: 25
backend: LMDB
}
}

我想出了这个正则表达式:

((layer)( *)((\n))*{((.*?)(\n)*)*(type)( *):( *)("Data")((.*?)(\n)*)*)(.*?)(\n)}

我试着模拟这个:

find and select a block starting with layer, 
there can be any number of space characters but after it
there should be a { character,
then there can be anything( for making it easier), and then
there should be a type followed by any number of spaces, then followed by "Data"
then anything can be there, until it is faced with a } character

但显然这不能正常工作。如果我更改任何这些层 block 中的类型,则不会检测到任何内容!甚至是具有 type : "Data"

的层

最佳答案

基于 this post关于使用 .net 正则表达式进行括号匹配,您可以调整所提供的正则表达式:

\((?>\((?<c>)|[^()]+|\)(?<-c>))*(?(c)(?!))\)

它正在寻找匹配的集合 ()你可以简单地将它们换成 {} (他们在该正则表达式中没有被转义)。

然后你可以在layer\s*前加前缀少量。

对于排除 block 的功能,其中 type <> "Data"我为所有其他 type 添加了负面前瞻pastebin 中示例中的关键字。不幸的是,为 type: "Data" 添加了一个积极的前瞻根本行不通,我认为如果行得通,那将是您最可靠的解决方案。

希望您有一个 type 的有限列表值,您可以扩展它以获得实用的解决方案:

layer\s*{(?>{(?<c>)|[^{}](?!type: "Accuracy"|type: "Convolution"|type: "Dropout"|type: "InnerProduct"|type: "LRN"|type: "Pooling"|type: "ReLU"|type: "SoftmaxWithLoss")+|}(?<-c>))*(?(c)(?!))}

原始正则表达式中要使用的关键位是 [^()]+它匹配正则表达式的其他组件匹配的括号之间的内容。我已将其改编为 [^{}]+ - 是“除大括号以外的所有内容”- 然后添加带有不匹配关键字的长“除此之外”子句。

关于c# - 我怎样才能在 C# 中获得这个正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com