Regular Expressions 101

Community Patterns

22

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

0

Regular Expression
Created·2019-01-18 07:29
Flavor·PCRE (Legacy)

/
((NUP?\s?\d{3,5}(\W?(E(C[AJKLMP]{1,2})?|M[AL\d]?))?(\W+(C\d|TVP\d|M\d|P\d{2}))?((\-|\+|\/)(C\d|H\s?\d{3,4}|R\d{3}))?)|(2\d{3,5}((\s|\-)?(CA?C?(K(JA|\d{2})?|N2|JA)?|MB|TN\d|E\d?([A๋CJKMWX]{1,2})?(TN9|D1)?|2RS|TVH?|SK?|KM?|M|TN))?((\/|\+|\-|\.)?(C\d8?(W\d{2,3})?|HA?\s?(\d{3,4}X?|\dC\dW\d{2})|2RS1K?TN9|TVH?|K(D1)?|M|W+\d{2,3}|VA\s?\d{3}))?((\-|\.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