String Matching in C #

I need a C # string search algorithm that can match multiple pattern occurrences. For example, if the pattern is “AA” and the string is “BAAABBB” Regex, the result of the result is Index = 1, but I need the result Index = 1,2. Can I get Regex to give this result?

+5
source share
6 answers

Use lookahead template: -

"A (? = A)"

This finds any A followed by another A without consuming the next A. Therefore, the AAA will match this pattern twice.

+13
source

To summarize all the previous comments:

Dim rx As Regex = New Regex("(?=AA)")
Dim mc As MatchCollection = rx.Matches("BAAABBB")

This will result in the result you are requesting.

EDIT:
# ( VB.NET, VB.NET).

Regex rx = new Regex("(?=AA)");
MatchCollection mc = rx.Matches("BAAABBB");
+4
0

:

       System.Text.RegularExpressions.MatchCollection  matchCol;
       System.Text.RegularExpressions.Regex regX = new System.Text.RegularExpressions.Regex("(?=AA)");

        string index="",str="BAAABBB"; 
        matchCol = regX.Matches(str);
        foreach (System.Text.RegularExpressions.Match mat in matchCol)
            {
                index = index + mat.Index + ",";
            }                       

- , , .

0

, ? , 20 , , ( , ). , , Boyer-Moore Knuth-Morris-Pratt, - , .

, , , ; .

0

All Articles