Regular Expressions 101

Community Patterns

23

Get path from any text

Created·2023-01-31 14:38
Updated·2023-07-23 20:17
Flavor·PCRE2 (PHP)
Recommended·
Get path (windows style) from any type of text (error message, e-mail corps ...), quoted or not. THIS IS THE SINGLE LINE VERSION ! If you want understand how it work or edit it, go https://regex101.com/r/7o2fyy Relative path are not supported The goal is to catch what "Look like" a path. See the limitations UNC path and prefix path like //./], [//?/] or [//./UNC/] are allowed some url path like [file:///C:/] or [file://] are allowed Catch path quoted with ["] and [']. But these quotes are include with the catch Quoted path is not concerned by limitations Limitations : (only unquoted path) [dot] and [space] is allowed, but not in a row [dot+space] or [space+dot at end of file name isn't catched INSIDE A NAME FILE (or last directory if it is a path to a directory) : [comma] is not supported (it stop the catch) after a first [dot], any [space] stop the catch after a [space], catch is stoped if next character is not a [letter], [digit] or [-] so, double [space] stop the catch Compatibility compatible PCRE, PCRE2 AutoHotkey : don't forget to escape "%" in "`%" /!\ Powershell and .Net /!\\ : this regex need some modification to be interpreted by powershell. You have to replace each (?&CapturGroupName) by \k. Use this powershell code to do this replacement : ` $powershellRegex = @' [Put here the regex to replace (?&CapturGroupName) with \k] '@ -replace '\(\?&(\w+)\)', '\k' ` This example code must return : [Put here the regex to replace \k with \k]
Submitted by nitrateag

Community Library Entry

1

Regular Expression
Created·2026-07-25 18:00
Flavor·.NET 7.0 (C#)

@"
(?<Comment>//.*|/\*.*?\*/)|(?<key>""[^""\\]*(?:\\.[^""\\]*)*"")(?=(?:\s|//.*|/\*.*?\*/)*:)|(?<value>(?<bool>true)|(?<bool>false)|(?<null>null)|""(?<string>[^""\\]*(?:\\.[^""\\]*)*)""|(?<number>-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?))|(?<value_sep>:)|(?<array_open>\[)|(?<array_sep>,)|(?<array_close>\])|(?<object_open>{)|(?<object_close>})|(?<whitespace>[^\S\r\n]+)|(?<newline>[\r\n]+)|(?<undefined>.+)
"
g
Open regex in editor

Description

Description

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<string, object>, object[], or primitives).

Regex Pattern

(?<Comment>//.*|/\*.*?\*/)|
(?<key>""[^""\\]*(?:\\.[^""\\]*)*"")(?=(?:\s|//.*|/\*.*?\*/)*:)|
(?<value>(?<bool>true)|(?<bool>false)|(?<null>null)|""(?<string>[^""\\]*(?:\\.[^""\\]*)*)""|(?<number>-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?))|
(?<value_sep>:)|
(?<array_open>\[)|
(?<array_sep>,)|
(?<array_close>\])|
(?<object_open>{)|
(?<object_close>})|
(?<whitespace>[^\S\r\n]+)|
(?<newline>[\r\n]+)|
(?<undefined>.+)

Named Capture Groups

Group NameMatches
CommentSingle‑line //… or multi‑line /*…*/ comments (skipped).
keyA JSON property key (double‑quoted string) followed by a colon (lookahead).
valueA JSON value – one of: true, false, null, a double‑quoted string, or a number (integer, float, or scientific).
boolSub‑group inside value for true/false (for direct parsing).
nullSub‑group for null.
stringSub‑group for the content inside double‑quotes (without the quotes).
numberSub‑group for numeric literals.
value_sepA colon : separating key and value.
array_openLeft bracket [.
array_sepComma , between array elements.
array_closeRight bracket ].
object_openLeft brace {.
object_closeRight brace }.
whitespaceHorizontal whitespace (spaces, tabs) – not newlines.
newlineLine‑break characters (CR, LF, CRLF).
undefinedAny 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