问题
With the following RegEx pattern:
(?<comment>(^#{2} [^\r\n]+[\s]+)*)(?:^\[(?:(?<hive>HK(?:LM|[DP]D|C[CUR]|U(SERS|SER|SR|S)))[:])?(?<name>[a-z0-9$][a-z0-9-_]{2,63})\])(?<items>[\S\s]*?)(?=\n{2,})
Parsing the following text:
[HKLM:Connection]
AuthKey = 0x8a79b42z67fct29b42e07b3fd78nc540
Url = https://dev.somewebsite.com
ApiPath = /api/
[HKLM:Settings]
AutoMinimizeConsole = no
StyleFile = Default
PhoneNbrs = [+]?[01]{0,3}[-. ]?[(]?[0-9][0-9][0-9][)]?[-. ]?[0-9][0-9][0-9][-. ]?[0-9][0-9][0-9][0-9]
PostalCodes = [ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy][0-9][ABCEGHJKLMNPRSTVWXYZabceghjklmnprstvwxyz][\s.-]?[0-9][ABCEGHJKLMNPRSTVWXYZabceghjklmnprstvwxyz][0-9]
[HKLM:Font-Mapping]
MonoSpaced = Courier New
User1 = Software Tester 7
User2 = Repetition Scrolling
User3 = basis333
[HKLM:UserInterface]
[HKCU:UserInterface]
[HKCU:Credentials]
Username =
Password? =
When entered into online Regex tests, the results come out as expected, but in code, no matches are found. The "data" variable used here is populated with the text provided above prior to this segment:
public const string GROUP_PATTERN = @"(?<comment>(^#{2} [^\r\n]+[\s]+)*)(?:^\[(?:(?<hive>HK(?:LM|[DP]D|C[CUR]|U(SERS|SER|SR|S)))[:])?(?<name>[a-z0-9$][a-z0-9-_]{2,63})\])(?<items>[\S\s]*?)(?=\n{2,})";
Regex groupParser = new Regex(GROUP_PATTERN, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Multiline);
MatchCollection matches = groupParser.Matches(data);
foreach (Match m in matches)
this.Add(IniGroupItem.Parse(m.Value));
At the inception of the ForEach, there are zero matches (should be six!)..
Since the pattern works on test sites, but not at all in c#, I don't know how to figure out what issues the compiler is having with it. Any insights / suggestions?
回答1:
The line endings in the majority online regex testers are LF only. Had you tested your .NET regex at the RegexStorm .NET regex tester you would have identified the issue quicker since its line endings are CRLF.
So, the problem is with (?=\n{2,}) as it requires a newline to repeat 2 or more times. Since there are two or more sequences of \r\n in the actual data you need to replace that pattern part with (?=(?:\r\n){2,}).
If you say (?=[\r\n]{3,}) works for you, it means you want to match a location followed with 3 or more LF or CR chars.
In mixed cases, if you want to match a place followed with 2 or more CLF or LF line break sequences, you may use (?=(?>\r\n?|\n){2,}).
来源:https://stackoverflow.com/questions/55434026/c-sharp-regex-pattern-works-in-online-testing-but-not-at-runtime