44using System . Linq ;
55using System . Reflection ;
66using System . Text ;
7- using System . Text . RegularExpressions ;
7+ using PCRE ;
88
99namespace GrokNet
1010{
@@ -13,14 +13,14 @@ public class Grok
1313 private readonly string _grokPattern ;
1414 private readonly Dictionary < string , string > _patterns ;
1515 private readonly Dictionary < string , string > _typeMaps ;
16- private Regex _compiledRegex ;
16+ private PcreRegex _compiledRegex ;
1717 private IReadOnlyList < string > _patternGroupNames ;
18- private const RegexOptions _defaultRegexOptions = RegexOptions . Compiled | RegexOptions . ExplicitCapture ;
19- private readonly RegexOptions _regexOptions ;
18+ private const PcreOptions _defaultRegexOptions = PcreOptions . Compiled | PcreOptions . ExplicitCapture ;
19+ private readonly PcreOptions _regexOptions ;
2020
21- private static readonly Regex _grokRegex = new Regex ( "%{(\\ w+):(\\ w+)(?::\\ w+)?}" , RegexOptions . Compiled ) ;
22- private static readonly Regex _grokRegexWithType = new Regex ( "%{(\\ w+):(\\ w+):(\\ w+)?}" , RegexOptions . Compiled ) ;
23- private static readonly Regex _grokWithoutName = new Regex ( "%{(\\ w+)}" , RegexOptions . Compiled ) ;
21+ private static readonly PcreRegex _grokRegex = new PcreRegex ( "%{(\\ w+):(\\ w+)(?::\\ w+)?}" , PcreOptions . Compiled ) ;
22+ private static readonly PcreRegex _grokRegexWithType = new PcreRegex ( "%{(\\ w+):(\\ w+):(\\ w+)?}" , PcreOptions . Compiled ) ;
23+ private static readonly PcreRegex _grokWithoutName = new PcreRegex ( "%{(\\ w+)}" , PcreOptions . Compiled ) ;
2424
2525 /// <summary>
2626 /// Initializes a new instance of the <see cref="Grok"/> class with the specified Grok pattern.
@@ -41,7 +41,7 @@ public Grok(string grokPattern)
4141 /// </summary>
4242 /// <param name="grokPattern">The Grok pattern to use.</param>
4343 /// <param name="regexOptions">Additional regex options.</param>
44- public Grok ( string grokPattern , RegexOptions regexOptions )
44+ public Grok ( string grokPattern , PcreOptions regexOptions )
4545 : this ( grokPattern )
4646 {
4747 _regexOptions = _defaultRegexOptions | regexOptions ;
@@ -65,7 +65,7 @@ public Grok(string grokPattern, Stream customPatterns)
6565 /// <param name="grokPattern">The Grok pattern to use.</param>
6666 /// <param name="customPatterns">A stream containing custom patterns.</param>
6767 /// <param name="regexOptions">Additional regex options.</param>
68- public Grok ( string grokPattern , Stream customPatterns , RegexOptions regexOptions )
68+ public Grok ( string grokPattern , Stream customPatterns , PcreOptions regexOptions )
6969 : this ( grokPattern , regexOptions )
7070 {
7171 LoadCustomPatterns ( customPatterns ) ;
@@ -84,12 +84,12 @@ public Grok(string grokPattern, IDictionary<string, string> customPatterns)
8484
8585 /// <summary>
8686 /// Initialized a new instance of the <see cref="Grok"/> class with specified Grok pattern,
87- /// custom patterns if necessary, and custom <see cref="RegexOptions "/> .
87+ /// custom patterns if necessary, and custom <see cref="PcreOptions "/> .
8888 /// </summary>
8989 /// <param name="grokPattern">The Grok pattern to use.</param>
9090 /// <param name="customPatterns">Custom patterns to add.</param>
9191 /// <param name="regexOptions">Additional regex options.</param>
92- public Grok ( string grokPattern , IDictionary < string , string > customPatterns , RegexOptions regexOptions )
92+ public Grok ( string grokPattern , IDictionary < string , string > customPatterns , PcreOptions regexOptions )
9393 : this ( grokPattern , regexOptions )
9494 {
9595 LoadCustomPatterns ( customPatterns ) ;
@@ -109,7 +109,7 @@ public GrokResult Parse(string text)
109109
110110 var grokItems = new List < GrokItem > ( ) ;
111111
112- foreach ( Match match in _compiledRegex . Matches ( text ) )
112+ foreach ( PcreMatch match in _compiledRegex . Matches ( text ) )
113113 {
114114 foreach ( string groupName in _patternGroupNames )
115115 {
@@ -157,14 +157,14 @@ private void ParsePattern()
157157 pattern = newPattern ;
158158 } while ( ! done ) ;
159159
160- _compiledRegex = new Regex ( pattern , _regexOptions ) ;
161- _patternGroupNames = _compiledRegex . GetGroupNames ( ) . ToList ( ) ;
160+ _compiledRegex = new PcreRegex ( pattern , _regexOptions ) ;
161+ _patternGroupNames = _compiledRegex . PatternInfo . GroupNames . ToList ( ) ;
162162 }
163163
164164 private void ProcessTypeMappings ( ref string pattern )
165165 {
166- MatchCollection matches = _grokRegexWithType . Matches ( string . IsNullOrEmpty ( pattern ) ? _grokPattern : pattern ) ;
167- foreach ( Match match in matches )
166+ IEnumerable < PcreMatch > matches = _grokRegexWithType . Matches ( string . IsNullOrEmpty ( pattern ) ? _grokPattern : pattern ) ;
167+ foreach ( PcreMatch match in matches )
168168 {
169169 _typeMaps . Add ( match . Groups [ 2 ] . Value , match . Groups [ 3 ] . Value ) ;
170170 }
@@ -258,25 +258,25 @@ private static void EnsurePatternIsValid(string pattern)
258258 {
259259 try
260260 {
261- _ = Regex . Match ( "" , pattern ) ;
261+ _ = PcreRegex . Match ( "" , pattern ) ;
262262 }
263263 catch ( Exception e )
264264 {
265265 throw new FormatException ( $ "Invalid regular expression { pattern } ", e ) ;
266266 }
267267 }
268268
269- private string ReplaceWithName ( Match match )
269+ private string ReplaceWithName ( PcreMatch match )
270270 {
271- Group group1 = match . Groups [ 2 ] ;
272- Group group2 = match . Groups [ 1 ] ;
271+ PcreGroup group1 = match . Groups [ 2 ] ;
272+ PcreGroup group2 = match . Groups [ 1 ] ;
273273
274274 return _patterns . TryGetValue ( group2 . Value , out var str ) ? $ "(?<{ group1 } >{ str } )" : $ "(?<{ group1 } >)";
275275 }
276276
277- private string ReplaceWithoutName ( Match match )
277+ private string ReplaceWithoutName ( PcreMatch match )
278278 {
279- Group group = match . Groups [ 1 ] ;
279+ PcreGroup group = match . Groups [ 1 ] ;
280280
281281 if ( _patterns . TryGetValue ( group . Value , out _ ) )
282282 {
0 commit comments