4
4
using System . Linq ;
5
5
using System . Reflection ;
6
6
using System . Text ;
7
- using System . Text . RegularExpressions ;
7
+ using PCRE ;
8
8
9
9
namespace GrokNet
10
10
{
@@ -13,14 +13,14 @@ public class Grok
13
13
private readonly string _grokPattern ;
14
14
private readonly Dictionary < string , string > _patterns ;
15
15
private readonly Dictionary < string , string > _typeMaps ;
16
- private Regex _compiledRegex ;
16
+ private PcreRegex _compiledRegex ;
17
17
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 ;
20
20
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 ) ;
24
24
25
25
/// <summary>
26
26
/// Initializes a new instance of the <see cref="Grok"/> class with the specified Grok pattern.
@@ -41,7 +41,7 @@ public Grok(string grokPattern)
41
41
/// </summary>
42
42
/// <param name="grokPattern">The Grok pattern to use.</param>
43
43
/// <param name="regexOptions">Additional regex options.</param>
44
- public Grok ( string grokPattern , RegexOptions regexOptions )
44
+ public Grok ( string grokPattern , PcreOptions regexOptions )
45
45
: this ( grokPattern )
46
46
{
47
47
_regexOptions = _defaultRegexOptions | regexOptions ;
@@ -65,7 +65,7 @@ public Grok(string grokPattern, Stream customPatterns)
65
65
/// <param name="grokPattern">The Grok pattern to use.</param>
66
66
/// <param name="customPatterns">A stream containing custom patterns.</param>
67
67
/// <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 )
69
69
: this ( grokPattern , regexOptions )
70
70
{
71
71
LoadCustomPatterns ( customPatterns ) ;
@@ -84,12 +84,12 @@ public Grok(string grokPattern, IDictionary<string, string> customPatterns)
84
84
85
85
/// <summary>
86
86
/// 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 "/> .
88
88
/// </summary>
89
89
/// <param name="grokPattern">The Grok pattern to use.</param>
90
90
/// <param name="customPatterns">Custom patterns to add.</param>
91
91
/// <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 )
93
93
: this ( grokPattern , regexOptions )
94
94
{
95
95
LoadCustomPatterns ( customPatterns ) ;
@@ -109,7 +109,7 @@ public GrokResult Parse(string text)
109
109
110
110
var grokItems = new List < GrokItem > ( ) ;
111
111
112
- foreach ( Match match in _compiledRegex . Matches ( text ) )
112
+ foreach ( PcreMatch match in _compiledRegex . Matches ( text ) )
113
113
{
114
114
foreach ( string groupName in _patternGroupNames )
115
115
{
@@ -157,14 +157,14 @@ private void ParsePattern()
157
157
pattern = newPattern ;
158
158
} while ( ! done ) ;
159
159
160
- _compiledRegex = new Regex ( pattern , _regexOptions ) ;
161
- _patternGroupNames = _compiledRegex . GetGroupNames ( ) . ToList ( ) ;
160
+ _compiledRegex = new PcreRegex ( pattern , _regexOptions ) ;
161
+ _patternGroupNames = _compiledRegex . PatternInfo . GroupNames . ToList ( ) ;
162
162
}
163
163
164
164
private void ProcessTypeMappings ( ref string pattern )
165
165
{
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 )
168
168
{
169
169
_typeMaps . Add ( match . Groups [ 2 ] . Value , match . Groups [ 3 ] . Value ) ;
170
170
}
@@ -258,25 +258,25 @@ private static void EnsurePatternIsValid(string pattern)
258
258
{
259
259
try
260
260
{
261
- _ = Regex . Match ( "" , pattern ) ;
261
+ _ = PcreRegex . Match ( "" , pattern ) ;
262
262
}
263
263
catch ( Exception e )
264
264
{
265
265
throw new FormatException ( $ "Invalid regular expression { pattern } ", e ) ;
266
266
}
267
267
}
268
268
269
- private string ReplaceWithName ( Match match )
269
+ private string ReplaceWithName ( PcreMatch match )
270
270
{
271
- Group group1 = match . Groups [ 2 ] ;
272
- Group group2 = match . Groups [ 1 ] ;
271
+ PcreGroup group1 = match . Groups [ 2 ] ;
272
+ PcreGroup group2 = match . Groups [ 1 ] ;
273
273
274
274
return _patterns . TryGetValue ( group2 . Value , out var str ) ? $ "(?<{ group1 } >{ str } )" : $ "(?<{ group1 } >)";
275
275
}
276
276
277
- private string ReplaceWithoutName ( Match match )
277
+ private string ReplaceWithoutName ( PcreMatch match )
278
278
{
279
- Group group = match . Groups [ 1 ] ;
279
+ PcreGroup group = match . Groups [ 1 ] ;
280
280
281
281
if ( _patterns . TryGetValue ( group . Value , out _ ) )
282
282
{
0 commit comments