コード例 #1
0
        private string ReplaceGroupNumber(Matcher match,
                                          string reformattedPattern,
                                          PatternGrouping patternGrouping,
                                          RegexOptions options)
        {
            int groupNumber     = int.Parse(match.group(1));
            int javaGroupNumber = groupNumber;
            int groupCount      = patternGrouping.GroupCount;

            if (groupCount == -1)
            {
                if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture)
                {
                    groupCount = 0;
                }
                else
                {
                    groupCount = JavaUtils.GroupCount(reformattedPattern);
                }
            }
            else
            {
                javaGroupNumber = patternGrouping.NetToJavaNumbersMap [groupNumber];
            }

            if (groupNumber > groupCount)
            {
                return(null);
            }

            return(match.replaceFirst(@"\\" + javaGroupNumber));
        }
コード例 #2
0
        internal static void ReplaceMatch(string replacementPattern, Matcher match, StringBuffer sb, string input, PatternData patternData)
        {
            replacementPattern = JavaUtils.ReplaceAll(replacementPattern, COPY_ENTIRE_MATCH_PATTERN, match.group());
            replacementPattern = JavaUtils.ReplaceAll(replacementPattern, INPUT_BEFORE_MATCH_PATTERN, input.Substring(0, match.start()));
            replacementPattern = JavaUtils.ReplaceAll(replacementPattern, INPUT_AFTER_MATCH_PATTERN, input.Substring(match.end()));
            replacementPattern = JavaUtils.ReplaceAll(replacementPattern, INPUT_PATTERN, input);

            int groupsNumber = match.groupCount();

            if (groupsNumber > 0)
            {
                Pattern p = Pattern.compile(LAST_CAPTURED_GROUP_PATTERN);
                Matcher m = p.matcher((CharSequence)(object)replacementPattern);
                if (m.find())
                {
                    while (groupsNumber > 0)
                    {
                        if (match.start(patternData.NetToJavaNumbersMap [groupsNumber]) >= 0)
                        {
                            break;
                        }
                        --groupsNumber;
                    }
                    if (groupsNumber > 0)
                    {
                        replacementPattern = m.replaceAll(match.group(patternData.NetToJavaNumbersMap [groupsNumber]));
                    }
                }
            }

            match.appendReplacement(sb, replacementPattern);
        }
コード例 #3
0
        public bool HasConstruct(string pattern, RegexOptions options)
        {
            if (JavaUtils.IsMatch(pattern, BACKSLASH_IN_CC_PATTERN))
            {
                //TODO Store result
                return(true);
            }
            if (JavaUtils.IsMatch(pattern, LEFT_FIGURE_PAREN))
            {
                //TODO Store result
                return(true);
            }
            if (JavaUtils.IsMatch(pattern, RIGHT_FIGURE_PAREN))
            {
                //TODO Store result
                return(true);
            }
            if (JavaUtils.IsMatch(pattern, BRACKET_IN_CC_PATTERN))
            {
                //TODO Store result
                return(true);
            }
            if (JavaUtils.IsMatch(pattern, NULL_PATTERN))
            {
                //TODO Store result
                return(true);
            }

            return(false);
        }
コード例 #4
0
        private static string ReformatPattern(string reformattedPattern)
        {
            //Reformat pattern
            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, REMOVED_NAME_PATTERN_TEMPL1, String.Empty);
            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, REMOVED_NAME_PATTERN_TEMPL2, String.Empty);
            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, REMOVED_NUMBERED_PATTERN_TEMPL1, String.Empty);
            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, REMOVED_NUMBERED_PATTERN_TEMPL2, String.Empty);

            return(reformattedPattern);
        }
コード例 #5
0
        public string Reformat(RegexOptions options,
                               string reformattedPattern,
                               PatternGrouping patternGrouping)
        {
            reformattedPattern = JavaUtils.ReplaceWithLookBehind(reformattedPattern, BACKSLASH_PATTERN, BACKSLASH_BEHIND_PATTERN, BACKSLASH);
            reformattedPattern = JavaUtils.ReplaceAllAdvanced(reformattedPattern, LEFT_FIGURE_PAREN, ESC_LEFT_FIGURE);
            reformattedPattern = JavaUtils.ReplaceAllAdvanced(reformattedPattern, RIGHT_FIGURE_PAREN, ESC_RIGHT_FIGURE);
            reformattedPattern = JavaUtils.ReplaceWithLookBehind(reformattedPattern, BRACKET_PATTERN, BRACKET_BEHIND_PATTERN, BRACKET);
            reformattedPattern = JavaUtils.ReplaceAllAdvanced(reformattedPattern, NULL_PATTERN, JAVA_NULL);

            return(reformattedPattern);
        }
コード例 #6
0
        public bool HasConstruct(string pattern, RegexOptions options)
        {
            if (JavaUtils.IsMatch(pattern, DEFINITION1))
            {
                return(true);
            }
            if (JavaUtils.IsMatch(pattern, DEFINITION2))
            {
                return(true);
            }

            return(false);
        }
コード例 #7
0
        public bool HasConstruct(string pattern, RegexOptions options)
        {
            if (JavaUtils.IsMatch(pattern, NUMBER_BACK_REFERENCE_PATTERN))
            {
                return(true);
            }

            if (JavaUtils.IsMatch(pattern, NAME_1_BACK_REFERENCE_PATTERN))
            {
                return(true);
            }

            return(JavaUtils.IsMatch(pattern, NAME_2_BACK_REFERENCE_PATTERN));
        }
コード例 #8
0
        public bool HasConstruct(string pattern, RegexOptions options)
        {
            if (JavaUtils.IsMatch(pattern, INLINE_COMMENT_PATTERN))
            {
                return(true);
            }

            if ((options & RegexOptions.IgnorePatternWhitespace) == 0)
            {
                return(false);
            }

            return(JavaUtils.IsMatch(pattern, COMMENT_PATTERN));
        }
コード例 #9
0
        internal static string Reformat(Regex regex, string replacement)
        {
            replacement = JavaUtils.ReplaceAll(replacement, BACKSLASH_PATTERN, JAVA_BACKSLASH);
            replacement = JavaUtils.ReplaceAll(replacement, DOUBLE_DOLLAR_PATTERN, JAVA_DOLLAR);

            Pattern p = Pattern.compile(SINGLE_DOLLAR_PATTERN);
            Matcher m = p.matcher((CharSequence)(object)replacement);

            StringBuffer sb = new StringBuffer();

            while (m.find())
            {
                if (m.start(1) >= 0)
                {
                    int groupNumber = regex.GroupNumberFromName(m.group(1));
                    if (groupNumber >= 0)
                    {
                        m.appendReplacement(sb, @"\$" + regex.GetJavaNumberByNetNumber(groupNumber));
                        continue;
                    }
                    if (int.TryParse(m.group(1), out groupNumber) && groupNumber <= regex.GroupCount)
                    {
                        m.appendReplacement(sb, @"\$" + regex.GetJavaNumberByNetNumber(groupNumber));
                        continue;
                    }

                    m.appendReplacement(sb, JAVA_DOLLAR + "{" + m.group(1) + "}");
                    continue;
                }
                if (m.start(2) >= 0)
                {
                    int netGroupNumber = int.Parse(m.group(2));
                    if (netGroupNumber > regex.GroupCount)
                    {
                        m.appendReplacement(sb, JAVA_DOLLAR + netGroupNumber);
                        continue;
                    }

                    m.appendReplacement(sb, @"\$" + regex.GetJavaNumberByNetNumber(netGroupNumber));
                    continue;
                }

                m.appendReplacement(sb, JAVA_DOLLAR);
            }

            m.appendTail(sb);

            return(sb.ToString());
        }
コード例 #10
0
        public string Reformat(RegexOptions options,
                               string reformattedPattern,
                               PatternGrouping patternGrouping)
        {
            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, INLINE_COMMENT_PATTERN, String.Empty);

            if ((options & RegexOptions.IgnorePatternWhitespace) == 0)
            {
                return(reformattedPattern);
            }

            reformattedPattern = JavaUtils.ReplaceAll(reformattedPattern, COMMENT_PATTERN, String.Empty);

            return(reformattedPattern);
        }
コード例 #11
0
        public string Reformat(RegexOptions options,
                               string reformattedPattern,
                               PatternGrouping patternGrouping)
        {
            if (!HasConstruct(reformattedPattern, options))
            {
                return(reformattedPattern);
            }

            if (patternGrouping.GroupCount >= 0 && patternGrouping.SameGroupsFlag)
            {
                return(null);
            }

            Matcher m = JavaUtils.Matcher(reformattedPattern, NUMBER_BACK_REFERENCE_PATTERN);

            if (m.find())
            {
                reformattedPattern = ReplaceGroupNumber(m, reformattedPattern, patternGrouping, options);
                if (reformattedPattern == null)
                {
                    return(null);
                }
            }

            m = JavaUtils.Matcher(reformattedPattern, NAME_1_BACK_REFERENCE_PATTERN);
            if (m.find())
            {
                reformattedPattern = ReplaceGroupName(m, reformattedPattern, patternGrouping, options);
                if (reformattedPattern == null)
                {
                    return(null);
                }
            }

            m = JavaUtils.Matcher(reformattedPattern, NAME_2_BACK_REFERENCE_PATTERN);
            if (m.find())
            {
                reformattedPattern = ReplaceGroupName(m, reformattedPattern, patternGrouping, options);
                if (reformattedPattern == null)
                {
                    return(null);
                }
            }

            return(reformattedPattern);
        }
コード例 #12
0
 public bool HasConstruct(string pattern, RegexOptions options)
 {
     if (JavaUtils.IsMatch(pattern, NAMED_GROUP_PATTERN_1))
     {
         return(true);
     }
     if (JavaUtils.IsMatch(pattern, NAMED_GROUP_PATTERN_2))
     {
         return(true);
     }
     if (JavaUtils.IsMatch(pattern, NUMBERED_GROUP_PATTERN_1))
     {
         return(true);
     }
     if (JavaUtils.IsMatch(pattern, NUMBERED_GROUP_PATTERN_2))
     {
         return(true);
     }
     return(false);
 }
コード例 #13
0
 public bool HasConstruct(string pattern, RegexOptions options)
 {
     return(JavaUtils.IsMatch(pattern, DEFINITION));
 }
コード例 #14
0
 public string Reformat(RegexOptions options,
                        string reformattedPattern,
                        PatternGrouping patternGrouping)
 {
     return(JavaUtils.ReplaceAll(reformattedPattern, DEFINITION, IS_CATEGORY));
 }
コード例 #15
0
        private static void UpdateGroupMapping(string reformattedPattern,
                                               RegexOptions options,
                                               PatternGrouping patternGrouping)
        {
            CharSequence workString = (CharSequence)(object)JavaUtils.ReplaceAll(reformattedPattern, ESCAPED_LEFT_PAREN_TEMPL, String.Empty);

            //Split pattern by left parenthesis
            Pattern p = Pattern.compile(LEFT_PAREN);

            string [] parts = p.split(workString);

            Pattern nonCapturedGroupPattern = Pattern.compile(NON_CAPTURED_GROUP_PATTERN);
            Pattern groupNamePattern1       = Pattern.compile(NAMED_GROUP_PATTERN1);
            Pattern groupNamePattern2       = Pattern.compile(NAMED_GROUP_PATTERN2);
            Pattern groupNumPattern1        = Pattern.compile(NUMBERED_GROUP_PATTERN1);
            Pattern groupNumPattern2        = Pattern.compile(NUMBERED_GROUP_PATTERN2);

            int enoughLength = parts.Length;

            string [] namedGroups = new string [enoughLength];
            int []    javaGroupNumberToNetGroupNumber = new int [enoughLength];
            int       capturedGroupsCount             = 0;
            int       namedGroupsCount   = 0;
            int       nonamedGroupsCount = 0;
            int       sameGroupsCounter  = 0;

            //Scan of groups
            for (int i = 1; i < parts.Length; ++i)
            {
                //nonamed group
                if (parts [i].StartsWith(QUESTION) == false)
                {
                    javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = ++nonamedGroupsCount;
                    continue;
                }

                //Skip non captured groups
                Matcher partMatcher =
                    nonCapturedGroupPattern.matcher((CharSequence)(object)parts [i]);
                if (partMatcher.find())
                {
                    continue;
                }

                //Find named groups by 2 patterns
                partMatcher = groupNamePattern1.matcher((CharSequence)(object)parts [i]);
                if (partMatcher.find())
                {
                    namedGroups [namedGroupsCount++] = partMatcher.group(1);
                    javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = -1;
                    continue;
                }
                partMatcher = groupNamePattern2.matcher((CharSequence)(object)parts [i]);
                if (partMatcher.find())
                {
                    namedGroups [namedGroupsCount++] = partMatcher.group(1);
                    javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = -1;
                    continue;
                }

                //Find explicitly numbered groups by 2 patterns
                partMatcher = groupNumPattern1.matcher((CharSequence)(object)parts [i]);
                if (partMatcher.find())
                {
                    int netGroupNumber = int.Parse(partMatcher.group(1));
                    if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture)
                    {
                        namedGroups [namedGroupsCount++] = partMatcher.group(1);
                        javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = -1;
                    }
                    else
                    {
                        javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = netGroupNumber;
                        if (javaGroupNumberToNetGroupNumber [capturedGroupsCount] != netGroupNumber)
                        {
                            ++sameGroupsCounter;
                        }
                    }
                    continue;
                }
                partMatcher = groupNumPattern2.matcher((CharSequence)(object)parts [i]);
                if (partMatcher.find())
                {
                    int netGroupNumber = int.Parse(partMatcher.group(1));
                    if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture)
                    {
                        namedGroups [namedGroupsCount++] = partMatcher.group(1);
                        javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = -1;
                    }
                    else
                    {
                        javaGroupNumberToNetGroupNumber [++capturedGroupsCount] = netGroupNumber;
                        if (javaGroupNumberToNetGroupNumber [capturedGroupsCount] != netGroupNumber)
                        {
                            ++sameGroupsCounter;
                        }
                    }
                    continue;
                }
            }

            //Filling grouping
            patternGrouping.SetGroups(namedGroups,
                                      javaGroupNumberToNetGroupNumber,
                                      nonamedGroupsCount,
                                      capturedGroupsCount,
                                      sameGroupsCounter,
                                      options);

            return;
        }