wiki:regex:index
Delphi 12 Athens Updates Available!
To download, click your product: DIContainers, DIConverters, DICreole, DIFileFinder, DIGoogleReader, DIHtmlLabel, DIHtmlParser, DIMime, DIRegEx, DISQLite3, DITidy, DIUcl, DIUnicode, DIXml, YuBrotli, YuImage, YuNetSurf, YuOpenSSL, YuPcre2, YuPdf, YuStemmer, YuXmlSec, YuZip.
To download, click your product: DIContainers, DIConverters, DICreole, DIFileFinder, DIGoogleReader, DIHtmlLabel, DIHtmlParser, DIMime, DIRegEx, DISQLite3, DITidy, DIUcl, DIUnicode, DIXml, YuBrotli, YuImage, YuNetSurf, YuOpenSSL, YuPcre2, YuPdf, YuStemmer, YuXmlSec, YuZip.
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | wiki:regex:index [2016/01/22 15:09] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== DIRegEx: Wiki ====== | ||
+ | {{page> | ||
+ | {{page>: | ||
+ | |||
+ | ===== Stack Overflow? ===== | ||
+ | |||
+ | **Question: Apparently, specific patterns with specific subjects can drive DIRegEx into stack overflow errors. How can I avoid these?** | ||
+ | |||
+ | Answer: DIRegEx uses a recursive matching algorithm which can run out of stack space with // | ||
+ | |||
+ | Even though stack overflows are a real problem, they happens so rarely with common regex patterns and subjects that most DIRegEx users will never notice. In case you ever do, these steps can help to avoid them. Obviously, all 3 options combined yield best results: | ||
+ | |||
+ | - Increase your application' | ||
+ | - Lower the TDIRegEx.MatchLimit and TDIRegEx.MatchLimitRecursion option properties. They cause matching to abort with PCRE_ERROR_MATCHLIMIT or PCRE_ERROR_RECURSIONLIMIT if the respective thresholds have been reached. The values can be set via TDIRegEx or the Extra field using the native API. | ||
+ | - Optimize your regular expression to avoid nested subpatterns with unlimited repeats. The section " | ||
+ | |||
+ | In Windows, the stack size is defined on a per-thread basis when the thread is created. This means that the calling thread' | ||
+ | |||
+ | If the calling application' | ||
+ | |||
+ | Unfortunately, | ||
+ | |||
+ | ===== TDIRegExSearchStream explained ===== | ||
+ | |||
+ | [[tdiregexstreamsearch_de|This page]] contains an interesting e-mail conversion about the internals of TDIRegExSearchStream and descendent classes (German / Deutsch). | ||
+ | |||
+ | ===== Sample RegEx search function ===== | ||
+ | |||
+ | This is sample function which shows if string contains a given regex or not. | ||
+ | |||
+ | <code delphi> | ||
+ | function RegExMatch(const Str, Re: string; ACaseSens: Boolean): Boolean; | ||
+ | var | ||
+ | RegEx: TDIRegEx; | ||
+ | begin | ||
+ | Result := False; | ||
+ | if (Str = '' | ||
+ | | ||
+ | RegEx := TDIPerlRegEx.Create(nil); | ||
+ | try | ||
+ | // | ||
+ | // | ||
+ | | ||
+ | if ACaseSens then | ||
+ | RegEx.CompileOptions := RegEx.CompileOptions - [coCaseLess] | ||
+ | else | ||
+ | RegEx.CompileOptions := RegEx.CompileOptions + [coCaseLess]; | ||
+ | RegEx.SetSubjectStr(Str); | ||
+ | RegEx.MatchPattern := Re; | ||
+ | Result := RegEx.Match(0) >= 0; | ||
+ | finally | ||
+ | RegEx.Free; | ||
+ | end; | ||
+ | end; | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Sample RegEx replace function ===== | ||
+ | |||
+ | This is sample function which replaces a regex ASearch with regex AReplace in string AValue. | ||
+ | |||
+ | <code delphi> | ||
+ | function RegExReplace( | ||
+ | const AValue: AnsiString; | ||
+ | const ASearch: AnsiString; | ||
+ | const AReplace: AnsiString; | ||
+ | const AOptions: TDIRegexCompileOptions = [coCaseLess]): | ||
+ | var | ||
+ | RE: TDIPerlRegEx; | ||
+ | begin | ||
+ | RE := TDIPerlRegEx.Create(nil); | ||
+ | try | ||
+ | RE.SetSubjectStr(AValue); | ||
+ | RE.CompileOptions := AOptions; | ||
+ | RE.CompileMatchPatternStr(ASearch); | ||
+ | RE.FormatPattern := AReplace; | ||
+ | if RE.Replace2(Result) = 0 then | ||
+ | Result := AValue; | ||
+ | finally | ||
+ | RE.Free; | ||
+ | end; | ||
+ | end; | ||
+ | </ | ||
+ | |||
+ | ===== Sample RegEx filling with a char ===== | ||
+ | |||
+ | This function returns original string, where all occurances of regex are filled with a char. | ||
+ | (e.g. " | ||
+ | |||
+ | <code delphi> | ||
+ | function RegExReplaceToChar( | ||
+ | const Str, Re: string; | ||
+ | ch: Char; ACaseSens: Boolean): string; | ||
+ | var | ||
+ | RegEx: TDIRegEx; | ||
+ | N_prev, N, i: Integer; | ||
+ | begin | ||
+ | Result := Str; | ||
+ | if (Str = '' | ||
+ | | ||
+ | RegEx := TDIPerlRegEx.Create(nil); | ||
+ | try | ||
+ | if ACaseSens then | ||
+ | RegEx.CompileOptions := RegEx.CompileOptions - [coCaseLess] | ||
+ | else | ||
+ | RegEx.CompileOptions := RegEx.CompileOptions + [coCaseLess]; | ||
+ | RegEx.MatchPattern := Re; | ||
+ | N_prev := -1; | ||
+ | repeat | ||
+ | RegEx.SetSubjectStr(Result); | ||
+ | if RegEx.Match(0) < 0 then Break; | ||
+ | N := RegEx.MatchedStrFirstCharPos + 1; | ||
+ | if N = N_prev then Break; | ||
+ | N_prev := N; | ||
+ | for i := N to (N + RegEx.MatchedStrLength - 1) do | ||
+ | Result[i] := ch; | ||
+ | until False; | ||
+ | finally | ||
+ | RegEx.Free; | ||
+ | end; | ||
+ | end; | ||
+ | </ |
wiki/regex/index.txt · Last modified: 2016/01/22 15:09 by 127.0.0.1