Example #1
0
        public static object FindIter(object pattern, object @string, int flags)
        {
            RE_Pattern pat = new RE_Pattern(ValidatePattern(pattern), flags);

            string str = ValidateString(@string, "string");

            return(MatchIterator(pat.FindAllWorker(str, 0, str.Length), pat));
        }
Example #2
0
        public static object FindAll(object pattern, string @string, int flags)
        {
            RE_Pattern pat = new RE_Pattern(ValidatePattern(pattern), flags);

            ValidateString(@string, "string");

            MatchCollection mc = pat.FindAllWorker(@string, 0, @string.Length);

            object[] matches = new object[mc.Count];
            int      numgrps = pat.re.GetGroupNumbers().Length;

            for (int i = 0; i < mc.Count; i++)
            {
                if (numgrps > 2)   // CLR gives us a "bonus" group of 0 - the entire expression
                //  at this point we have more than one group in the pattern;
                //  need to return a list of tuples in this case

                //  for each match item in the matchcollection, create a tuple representing what was matched
                //  e.g. findall("(\d+)|(\w+)", "x = 99y") == [('', 'x'), ('99', ''), ('', 'y')]
                //  in the example above, ('', 'x') did not match (\d+) as indicated by '' but did
                //  match (\w+) as indicated by 'x' and so on...
                {
                    int       k   = 0;
                    ArrayList tpl = new ArrayList();
                    foreach (Group g in mc[i].Groups)
                    {
                        //  here also the CLR gives us a "bonus" match as the first item which is the
                        //  group that was actually matched in the tuple e.g. we get 'x', '', 'x' for
                        //  the first match object...so we'll skip the first item when creating the
                        //  tuple
                        if (k++ != 0)
                        {
                            tpl.Add(g.Value);
                        }
                    }
                    matches[i] = Tuple.Make(tpl);
                }
                else if (numgrps == 2)
                {
                    //  at this point we have exactly one group in the pattern (including the "bonus" one given
                    //  by the CLR
                    //  skip the first match since that contains the entire match and not the group match
                    //  e.g. re.findall(r"(\w+)\s+fish\b", "green fish") will have "green fish" in the 0
                    //  index and "green" as the (\w+) group match
                    matches[i] = mc[i].Groups[1].Value;
                }
                else
                {
                    matches[i] = mc[i].Value;
                }
            }

            return(new List(matches));
        }
Example #3
0
        public static object findall(CodeContext/*!*/ context, object pattern, string @string, int flags) {
            RE_Pattern pat = new RE_Pattern(context, ValidatePattern(pattern), flags);
            ValidateString(@string, "string");

            MatchCollection mc = pat.FindAllWorker(context, @string, 0, @string.Length);
            return FixFindAllMatch(pat, mc);
        }
Example #4
0
        public static object finditer(CodeContext/*!*/ context, object pattern, object @string, int flags) {
            RE_Pattern pat = new RE_Pattern(context, ValidatePattern(pattern), flags);

            string str = ValidateString(@string, "string");
            return MatchIterator(pat.FindAllWorker(context, str, 0, str.Length), pat, str);
        }
Example #5
0
        public static object FindIter(object pattern, object @string, int flags)
        {
            RE_Pattern pat = new RE_Pattern(ValidatePattern(pattern), flags);

            string str = ValidateString(@string, "string");
            return MatchIterator(pat.FindAllWorker(str, 0, str.Length), pat, str);
        }