"" <> () {} [] (), types of punctuation in uncertain, but is a must in pairs,
"Childhood" practice questions (with answers) _ (founded baidu library)
"Childhood exercises (with answers) _ baidu library was not
"Childhood" exercises (including the answer _ baidu library was not
"Childhood" exercises [contain the answer] _ (baidu library was not
"Childhood" exercises [contain the answer] _ (baidu library) is founded
CodePudding user response:
Regular is not the best choice, it requires balance group, writing is not good maintenance, trouble
Used to analyze status code, as long as a few lines of code, faster,
The static bool IsBalanced (string input) { Const string pairs="" "<> () {} [] () ". Var counters=new int [pairs. Length/2]; Input The Select (x=& gt; Pairs. IndexOf (x)) The Where (x=& gt; X & gt;=0) ToList () The ForEach (x=& gt; Counters/x/2 +=((x & amp; 1) * 2-1)); Return counters. The Sum ()==0; } CodePudding user response:
correct (# 1) : The static bool IsBalanced (string input)
{
.
Return couters.
All (x=& gt; X==0) ;
}
CodePudding user response:
reference 1st floor github_36000833 response: regular is not the best choice, it requires balance group, writing is not good maintenance, trouble Used to analyze status code, as long as a few lines of code, faster, The static bool IsBalanced (string input) { Const string pairs="" "<> () {} [] () ". Var counters=new int [pairs. Length/2]; Input The Select (x=& gt; Pairs. IndexOf (x)) The Where (x=& gt; X & gt;=0) ToList () The ForEach (x=& gt; Counters/x/2 +=((x & amp; 1) * 2-1)); Return counters. The Sum ()==0; } According to tip the balance of above groups, from already groping, added some conditions, the line does not match the Spaces,,,, Such as [^,,,,,?; "" "" <> () () () [] {} \ s] * (((? 'jxfinest' (" | | "" | | \ [| (| | ({) [^,,,,,?;" "" "<> () () () [] {} \ s] *) + ((? '- jxfinest' (" | | ">) | | \) | |]) |})) [^,,,,,?;" "" "<> () () () [] {} \ s] *) +) * (? ((jxfinest)? !). ) CodePudding user response:
CodePudding user response:
reference jxfinest reply: 3/f ... According to tip the balance of above groups, from already groping, added some conditions, the line does not match the Spaces,,,, Such as . Encourage and praise a, Symbol if you want to match, you may use the more balance group, only total match, cannot solve the following input: "childhood library) With regular purpose, is to make development easier, if found that regular more trouble, it is time to consider other way, CodePudding user response:
reference 5 floor github_36000833 reply: Quote: refer to the third floor jxfinest response: . According to tip the balance of above groups, from already groping, added some conditions, the line does not match the Spaces,,,, Such as . Encourage and praise a, Symbol if you want to match, you may use the more balance group, only total match, cannot solve the following input: "childhood library) With regular purpose, is to make development easier, if found that regular more trouble, it is time to consider other practices, You mentioned: "childhood library) I remember I was considered the situation of ah, but there are no computer validation National Day go home in the car now, I am here is (" | | ">) | | | | | \)])}) Rather than [" ">) \])}] CodePudding user response:
Verified, do exist github_36000833 described BUG CodePudding user response:
Using multiple balance group, it is possible to each group of balance, (? X) # allow blank and comments ^ ( [^ "" <> () {} [] ()] # does not contain special symbols | "(? ) | "(? <- dab>) # see "", the corresponding increase or decrease in dab (Double Angle Bracket) balance group | <(? ) |> (? <- ab>) | \ [(? ) | \] (? <- p>) | {(? ) |} (? <- cb>) | [(? & lt; sqb>) |] (? <- sqb>) | ((? ) (|)? <- cnb>) ) * (? ((dab)? !). Balance) # check Double Angle Bracket set allowance is not zero (? (ab)? !). ) # check the Angle Bracket balance group (? (p)? !). ) # check the Parenthesis balance group (? ((cb)? !). ) # check CurlyBrace balance group (? ((SQB)? !). ) # check SquareBracket balance group (? ((CNB)? !). ) # check CornerBracket balance group $
So that we can maintain a balance between each group, such as "Childhood [exercises with answers]" nevertheless, staggered match will be improved, which also can't judge the following cross match (if need strict matching) : "Childhood [exercises with answers"] In code is easier to control: class Program { The static void Main (string [] args) { Var b1=IsBalanced (" "childhood" exercises [contain the answer] _ (baidu library) set up ");//true Var b2=IsBalanced (" "" "" "" childhood" contain the solution [] (baidu library) "" "" "");//true Var b3=IsBalanced (library) "" childhood");//false Var b4=IsBalanced (" "childhood [exercises with answers"] ");//false; } The static bool IsBalanced (string input) { Const string pairs="" "<> () {} [] () ". Var stack=new Stack (a); The foreach (char c in input) { Int I=pairs. IndexOf (c); If (I==1) { continue;//not symbolic, skip } If (I % 2==0) { Stack. Push (c);//even as symbolic open, into the stack } End the else//odd for symbols, check the top of the stack must be matched 'symbol' { If (stack. Count==0 | | stack. Pop ()!=pairs] [I - 1) { return false; } } } The return stack. Count==0;//all pairs must complete } }