private void FillGroups(RegexOptions options) { if (_patternGrouping.GroupCount >= 0) { return; } Matcher m = JavaPattern.matcher((CharSequence)(object)String.Empty); if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture) { _patternGrouping.SetGroups(0, m.groupCount()); } else { _patternGrouping.SetGroups(m.groupCount(), m.groupCount()); } }
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; }
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; }