Regular Expressions 101

Community Library

2

JSON Parser for .NET

Created·2026-07-25 18:00
Updated·2026-08-01 19:10
Type·Match
Flavor·.NET 7.0 (C#)
JSON Parser This regular expression is designed to tokenize JSON-like content embedded in INI files. It is not a full JSON validator; instead, it provides a lightweight lexical scanner that identifies structural tokens (objects, arrays, strings, numbers, booleans, null) and ignores comments and whitespace. The token stream is then processed by a hand‑written recursive‑descent parser (with depth‑limit protection) to build a .NET object graph (Dictionary, object], or primitives). Regex Pattern (?//.|/\.?\/)| (?""(?:\\.)"")(?=(?:\s|//.|/\.?\/):)| (?(?true)|(?false)|(?null)|""(?(?:\\.))""|(?-?(?:0|[1-9)(?:\.0-9]+)?(?:[eE?[0-9]+)?))| (?:)| (?\[)| (?,)| (?\])| (?{)| (?})| (?+)| (?[\r\n]+)| (?.+) Named Capture Groups | Group Name | Matches | |------------------|-------------------------------------------------------------------------| | comment | Single‑line //… or multi‑line /…/ comments (skipped). | | key | A JSON property key (double‑quoted string) followed by a colon (lookahead). | | value | A JSON value – one of: true, false, null, a double‑quoted string, or a number (integer, float, or scientific). | | bool | Sub‑group inside value for true/false (for direct parsing). | | null | Sub‑group for null. | | string | Sub‑group for the content inside double‑quotes (without the quotes). | | number | Sub‑group for numeric literals. | | value_sep | A colon : separating key and value. | | array_open | Left bracket [. | | array_sep | Comma , between array elements. | | array_close | Right bracket ]. | | object_open | Left brace {. | | object_close | Right brace }. | | whitespace | Horizontal whitespace (spaces, tabs) – not newlines. | | newline | Line‑break characters (CR, LF, CRLF). | | undefined | Any other character (should not occur in valid JSON; used as fallback). | Important Notes No recursion – the regex only tokenises; the parser handles nesting and depth limits. Escaped characters inside strings (\n, \t, \", etc.) are not unescaped by the regex – the parser calls UnEscape() when _allowEscapeChars is true. Whitespace and newlines are ignored by the parser (skipped during token iteration). The undefined group uses .+ (not .*) to avoid matching empty positions – this prevents false positives when the scanner reaches the end of the string. Purpose This regex is a solid foundation for building a custom JSON lexer, parser, or tokeniser for .NET projects. Its clear separation of structural elements, comments, and whitespace makes it easy to implement lightweight, hand‑crafted parsers that do not rely on heavy external libraries. It is particularly well‑suited for small to medium‑sized files, configuration blocks, or embedded data fragments, where performance and memory footprint matter. You can adapt the token stream to your own data model, add validation, or transform the JSON on the fly – all while keeping full control over the parsing logic.
Submitted by Pavel Bashkardin
1

Paragraph Delimiter Counter (Unicode-Aware)

Created·2024-12-05 02:56
Updated·2024-12-05 03:24
Type·Match
Flavor·.NET 7.0 (C#)
Finds all paragraphs in the input text, where a paragraph is defined as any occurrence of a non-whitespace character immediately following any of the following and any other preceding whitespace: 2 or more consecutive CRLF sequences 2 or more consecutive CR characters 2 or more consecutive LF characters 1 or more Unicode Paragraph Separator class characters The beginning of the string (matches the first paragraph) Again, note that whitespace mixed in with the above will not interfere with the matching, as demonstrated by the test text included. This is intended to be used with the options specified, so be sure to include them for best performance (non-backtracking, multiline, non-capturing, invariant culture). This will work effectively on any version of .net that supports the included syntax. However, it is intended for use with .net8.0 and up, with the Regex.EnumerateMatches() method, or, more ideally, with .net9.0 and up, using the new Regex.EnumerateSplits() method, to avoid allocations associated with Match objects. Unicode paragraph separator characters are very rare in practice and support for them is almost non-existent in software, including the Windows Console. Windows Terminal, web browsers, the Windows clipboard, notepad, Visual Studio, and notepad++, all of which fail to handle it in their own ways, none of them actually adding a line when they occur (though notepad++ will show it as PS if you have enabled showing all whitespace). It is safe to remove |\p{Zp}+ from the pattern, if you do not wish to include those characters in your search. The resulting pattern, as a c# string, would be: "((\\r\\n|\\r|\\n){2,}|\\A)^\\s*\\S"
Submitted by dodexahedron
1

INI Parser for .NET

Created·2024-04-03 08:20
Updated·2026-08-01 19:08
Type·Match
Flavor·.NET 7.0 (C#)
INI Parser A .NET regular expression for tokenizing INI-style configuration files. Features Supports: Sections: [Section] Key-value entries: key=value and key:value Comments: # comment and ; comment Whitespace and line breaks Multiline object blocks using { ... } Nested braces inside object blocks C-style comments inside object blocks Captured groups | Group | Description | |---|---| | text | Any meaningful text token | | comment | Comment line | | open | Opening symbol (#, ;, [) | | close | Closing symbol (]) | | section | Section declaration | | entry | Key-value entry | | key | Entry key | | delimiter | Key-value separator (= or :) | | value | Section name, comment text, or entry value | | undefined | Unknown text | | linebreaker | Line ending | | whitespace | Spaces and tabs | Multiline values Values wrapped in { } are treated as a single block: Json = { "name": "example", "items": [ 1, 2 ] } Inside blocks: strings are ignored when counting braces; // comments are ignored; /* comments */ are ignored; nested {} are supported. Limitations This regex performs structural parsing only. It does not validate: JSON syntax; duplicate keys; value types; application-specific rules. Use a dedicated parser after tokenization if full validation is required. Compatibility Designed for: .NET System.Text.RegularExpressions balancing groups support Not compatible with regex engines without balancing group support.
Submitted by Pavel Bashkardin

Community Library Entry

0

Regular ExpressionOpen Workspace

/
^(?:[A-Za-z0-9_][A-Za-z0-9_\-]*(?:\.[A-Za-z0-9_\-]+)*\/)+[A-Za-z0-9_][A-Za-z0-9_\-]*(?:\.[A-Za-z0-9_\\-]+)*$
/
gm

Description
Created·2016-08-01 08:19
Type·Match
Flavor·PCRE (Legacy)

no description available

Submitted by Anonymous
Open Workspace