Example #1
0
 private static IEnumerator MatchIterator(MatchCollection matches, RE_Pattern pattern)
 {
     for (int i = 0; i < matches.Count; i++)
     {
         yield return(RE_Match.make(matches[i], pattern));
     }
 }
Example #2
0
 internal static RE_Match makeMatch(Match m, RE_Pattern pattern, int offset)
 {
     if (m.Success && m.Index == offset)
     {
         return(new RE_Match(m, pattern));
     }
     return(null);
 }
Example #3
0
 internal static RE_Match make(Match m, RE_Pattern pattern)
 {
     if (m.Success)
     {
         return(new RE_Match(m, pattern));
     }
     return(null);
 }
Example #4
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 #5
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 #6
0
 public RE_Match(Match m, RE_Pattern pattern)
 {
     this.m       = m;
     this.pattern = pattern;
     for (int i = 0; i < m.Groups.Count; i++)
     {
         if (m.Groups[i].Captures.Count > 0)
         {
             lastindex = i;
         }
     }
 }
Example #7
0
        private static string ValidatePattern(object pattern)
        {
            if (pattern is string)
            {
                return(pattern as string);
            }

            ExtensibleString es = pattern as ExtensibleString;

            if (es != null)
            {
                return(es.Value);
            }

            RE_Pattern rep = pattern as RE_Pattern;

            if (rep != null)
            {
                return(rep.pre.UserPattern);
            }

            throw Ops.TypeError("pattern must be a string or compiled pattern");
        }
Example #8
0
 private static IEnumerator MatchIterator(MatchCollection matches, RE_Pattern pattern, string input) {
     for (int i = 0; i < matches.Count; i++) {
         yield return RE_Match.make(matches[i], pattern, input, 0, input.Length);
     }
 }
Example #9
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);
        }
Example #10
0
 public RE_Match(Match m, RE_Pattern pattern, string text) {
     _m = m;
     _pattern = pattern;
     _text = text;
 }
Example #11
0
 internal static RE_Match makeMatch(Match m, RE_Pattern pattern, string input, int offset, int endpos) {
     if (m.Success && m.Index == offset) return new RE_Match(m, pattern, input, offset, endpos);
     return null;
 }
Example #12
0
 internal static RE_Match make(Match m, RE_Pattern pattern, string input) {
     if (m.Success) return new RE_Match(m, pattern, input, 0, input.Length);
     return null;
 }
Example #13
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 #14
0
        private static object FixFindAllMatch(RE_Pattern pat, MatchCollection mc) {
            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;
                    List<object> tpl = new List<object>();
                    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] = PythonTuple.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 List.FromArrayNoCopy(matches);
        }
Example #15
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 #16
0
 public RE_Match(Match m, RE_Pattern pattern, string text)
 {
     this.m = m;
     this.pattern = pattern;
     this.text = text;
 }
Example #17
0
 public RE_Match(Match m, RE_Pattern pattern, string text, int pos, int endpos) {
     _m = m;
     _pattern = pattern;
     _text = text;
     _pos = pos;
     _endPos = endpos;
 }
Example #18
0
        public static object FindAll(object pattern, string @string, int flags)
        {
            RE_Pattern pat = new RE_Pattern(ValidatePattern(pattern), flags);
            ValidateString(@string, "string");

            return pat.FindAll(@string, 0, @string.Length);
        }