I created a matching pattern in RegexBuddy that behaves exactly as I expect. But I can not pass this to Delphi XE, at least when using the latest built-in TRegEx or TPerlRegEx.
There are 6 capture groups in my real world code, but I can illustrate the problem in a lighter example. This code gives β3β in the first dialog box, and then throws an exception (-7 index outside) when the second dialog is executed.
var Regex: TRegEx; M: TMatch; begin Regex := TRegEx.Create('(?P<time>\d{1,2}:\d{1,2})(?P<judge>.{1,3})'); M := Regex.Match('00:00 X1 90 55KENNY BENNY'); ShowMessage(IntToStr(M.Groups.Count)); ShowMessage(M.Groups['time'].Value); end;
But if I use only one capture group
Regex := TRegEx.Create('(?P<time>\d{1,2}:\d{1,2})');
β2β is displayed in the first dialog box, and β00:00β time is displayed in the second dialog box, as expected.
However, this would be a little limited if only one group of named captures were allowed, but that is not so ... If I change the name of the capture group, for example, to "atime".
var Regex: TRegEx; M: TMatch; begin Regex := TRegEx.Create('(?P<atime>\d{1,2}:\d{1,2})(?P<judge>.{1,3})'); M := Regex.Match('00:00 X1 90 55KENNY BENNY'); ShowMessage(IntToStr(M.Groups.Count)); ShowMessage(M.Groups['atime'].Value); end;
I will get "3" and "00:00" as expected. Are there any reserved words that I cannot use? I donβt think so, because in my real example I tried completely random names. I just can't figure out what causes this behavior.
regex delphi delphi-xe regexbuddy
Stefan fjellsten
source share