我有以下正则表达式:https://regex101.com/r/9gElou/2
在这里,我想使用regex获得单独的c#类,我的意思是我想匹配:
[XmlRoot(ElementName="settings")]
public sealed class Settings {
[XmlElement(ElementName="gazelle_description")]
public string Gazelle_description { get; set; }
[XmlElement(ElementName="gazelle_authkey")]
public string Gazelle_authkey { get; set; }
[XmlElement(ElementName="gazelle_pass")]
public string Gazelle_pass { get; set; }
} 然后
[XmlRoot(ElementName="server")]
public sealed class Server {
[XmlAttribute(AttributeName="network")]
public string Network { get; set; }
[XmlAttribute(AttributeName="serverNames")]
public string ServerNames { get; set; }
[XmlAttribute(AttributeName="channelNames")]
public string ChannelNames { get; set; }
[XmlAttribute(AttributeName="announcerNames")]
public string AnnouncerNames { get; set; }
}分开。
我的regex匹配整个类,而不是单独获取它们。
我的错误在哪里:
\[XmlRoot\(ElementName="\w+"\)\]\s+public sealed class \w+ \{(\s+[\]\(\)\w\d_="\[\{\};<>]+)+?
发布于 2019-09-17 09:13:31
用这个:
\[XmlRoot\(ElementName="\w+"\)\]\s+public sealed class \w+ \{[\]\(\)\w\d="\[\{\};<>\s]+?\}\s*\}
这个字符集需要懒惰地匹配。请参阅上面+?的用法。而且,类的末尾被定义为两个},它们之间有或没有空格- \}\s*\}。
此外,
\w已经包含了_,所以我将其从字符集中删除。\s被添加到字符集中。https://stackoverflow.com/questions/57970793
复制相似问题