Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex has pattern errors, and here's a fix #1

Open
ciphernemo opened this issue Jan 17, 2025 · 1 comment
Open

Regex has pattern errors, and here's a fix #1

ciphernemo opened this issue Jan 17, 2025 · 1 comment

Comments

@ciphernemo
Copy link

First, I wanted to thank you for creating this! I needed an INI reading/writing solution for C# and this gave me a great starting point. Your C# file is quite robust. Much appreciated!

Your regular expression's logic works fine, but all subpattern names mus be unique to prevent pattern errors. I'm not sure if this causes issues for the way your c# code works, but it will cause issues if this is ever expanded to include additional subpatterrn matches in the same function where you're trying to identify them by name.

Here's the updated regular expression to get around this issue:

(?=\S)(?<text>(?<comment>(?<open_comment>[#;]+)(?:[^\S\r\n]*)(?<value>.+))|(?<section>(?<open_section>\[)(?:\s*)(?<value_section>[^\]]*\S+)(?:[^\S\r\n]*)(?<close>\]))|(?<entry>(?<key>[^=\r\n\ [\]]*\S)(?:[^\S\r\n]*)(?<delimiter>:|=)(?:[^\S\r\n]*)(?<value_entry>[^#;\r\n]*))|(?<undefined>.+))(?<=\S)|(?<linebreaker>\r\n|\n)|(?<whitespace>[^\S\r\n]+)

And here it is in regex101: https://regex101.com/r/x37Qua/1

@ng256
Copy link
Owner

ng256 commented Jan 18, 2025

It's true that some regular expression languages do not allow duplicate identifiers. However, C#, like most other programming languages, does not have this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants