C# 用于字符串验证的正则表达式(带有字符标记)
我需要一个正则表达式来验证字符串C# 用于字符串验证的正则表达式(带有字符标记),c#,regex,C#,Regex,我需要一个正则表达式来验证字符串 string test = "C:\Dic\<:Id:>.<:Dic:>testtest<:Location:>.Test.doc" string test=“C:\Dic\.testtest.test.doc” 我使用正则表达式获取“”之间的所有字段 Regex.Matches(fileNameConfig,@“\”) .Cast() .Select(m=>m.Groups[0].Value).ToList(); 现在
string test = "C:\Dic\<:Id:>.<:Dic:>testtest<:Location:>.Test.doc"
string test=“C:\Dic\.testtest.test.doc”
我使用正则表达式获取“”之间的所有字段
Regex.Matches(fileNameConfig,@“\”)
.Cast()
.Select(m=>m.Groups[0].Value).ToList();
现在,我需要检查是否有任何打开的标记没有关闭标记,是否有任何嵌套的标记
string test = "C:\Dic\<:<:Id:>.<:Dic:>testtest<:Location:>.Test.doc"
string test = "<:C:\Dic\<:Id:>.<:Dic:>testtest<:Location:>.Test.doc:>"
string test=“C:\Dic\可以通过计算打开和关闭括号来测试嵌套
在管柱的任何位置,该位置之前的开口括号数必须大于或等于闭合括号数
在字符串末尾,开始括号的数量必须正好等于结束括号的数量
public static bool IsBracketNestingValid(string input) {
if (string.IsNullOrWhiteSpace(input)) {
return true; // empty string is always nested correctly
}
const string openingBracket = "<:";
const string closingBracket = ":>";
if (input.Length < openingBracket.Length) {
// perform this check if we expect that input strings
// must contain at least one bracket (e.g. "<" should be invalid)
return false;
}
int openingCount = 0;
int closingCount = 0;
for (int pos = 0; pos < input.Length-1; pos++) {
string currentToken = string.Format("{0}{1}", input[pos], input[pos+1]);
if (currentToken == openingBracket) {
openingCount++;
// skip over this recognized token
// (so we do not count any ':' twice, e.g. "<:>" should be invalid)
pos++;
}
if (currentToken == closingBracket) {
closingCount++;
pos++; // skip over this recognized token
}
if (closingCount > openingCount) {
return false; // found closing bracket before opening bracket
}
}
return openingCount == closingCount;
}
public static bool IsBracketNestingValid(字符串输入){
if(string.IsNullOrWhiteSpace(输入)){
return true;//空字符串总是正确嵌套
}
常量字符串openingBracket=“”;
if(input.Length //必须至少包含一个括号(例如“通常”,因为“语言”不支持嵌套结构。虽然.NET正则表达式允许平衡结构,但在这里如何使用它们还不清楚。要求是什么?正如我写的,我需要从字符串中获取“”之间的子字符串,但在检查所有标记是否正确之前
public static bool IsBracketNestingValid(string input) {
if (string.IsNullOrWhiteSpace(input)) {
return true; // empty string is always nested correctly
}
const string openingBracket = "<:";
const string closingBracket = ":>";
if (input.Length < openingBracket.Length) {
// perform this check if we expect that input strings
// must contain at least one bracket (e.g. "<" should be invalid)
return false;
}
int openingCount = 0;
int closingCount = 0;
for (int pos = 0; pos < input.Length-1; pos++) {
string currentToken = string.Format("{0}{1}", input[pos], input[pos+1]);
if (currentToken == openingBracket) {
openingCount++;
// skip over this recognized token
// (so we do not count any ':' twice, e.g. "<:>" should be invalid)
pos++;
}
if (currentToken == closingBracket) {
closingCount++;
pos++; // skip over this recognized token
}
if (closingCount > openingCount) {
return false; // found closing bracket before opening bracket
}
}
return openingCount == closingCount;
}