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

0

Regular Expression
Created·2019-10-03 21:33
Flavor·PCRE (Legacy)

/
(?# PCRE 8.x ) ^ (?<scheme> (?i) [a-z0-9+.-]+ (?-i) ) : (?:\/\/ (?<authority> (?<userinfo> (?<username> (?&uriUnreserved)+ ) (?: : (?<password> (?&uriUnreserved)+ )? )? @ )? (?<host> | (?'ipv4' (?&_ipv4) ) | \[ (?'ipv6' (?&_ipv6) ) \] | (?<hostname> (?i) (?: (?&hostLabel) \. )* (?&hostLabel) (?-i) ) ) (?: : (?<port> (?&port_number) ) )? ) )? (?# If an authority component is present, then the path component must either be empty or begin with a slash. If an authority component is absent, then the path cannot begin with an empty segment, that is with two slashes as the following characters would be interpreted as an authority component. ) (?# TODO: Exception: file URI scheme RFC 8089 https://en.wikipedia.org/wiki/File_URI_scheme ) (?<path> (?(authority) (?= [\/?#] | $ ) | (?! \/\/ ) ) (?&pathSequence) ) (?: \? (?'query'(?&_query)) )? (?: \# (?<fragment>(?&_fragment)))? $ (?(DEFINE) (?# http://es5.github.io/#A.6 ) (?<uriCharacter> (?&uriReserved) | (?&uriUnescaped) | (?&uriEscaped) ) (?<uriReserved> [;\/?:@&=+$,] ) (?<uriUnescaped> [[:alpha:]\d] | (?&uriMark) ) (?<uriEscaped> %[[:xdigit]]{2} ) (?<uriMark> [-_.!~*`()] ) (?<uriUnreserved> (?&uriUnescaped) | (?&uriEscaped) ) (?# https://en.wikipedia.org/wiki/Uniform_Resource_Identifier ) (?# allowed characters per RFC 952, RFC 1123 ) (?<hostLabel> [a-z0-9-]{1,63} ) (?<_ipv4> (?: (?&octet) \. ){3} (?&octet) ) (?<octet> (?&_250_255) | (?&_200_249) | (?&_0_199) ) (?<_250_255> 25[0-5] ) (?<_200_249> 2[0-4]\d ) (?<_0_199> 1? (?&_0_99) ) (?<_0_99> [1-9]? \d ) (?<_ipv6> (?&ipv6_2) ) (?<ipv6_1> (?&hextet) :: (?: (?&hextet) : ){0,5} (?&hextet) ) (?<ipv6_2> (?&hextet) : (?&hextet) :: (?: (?&hextet) : ){0,4} (?&hextet) ) (?<ipv6_3> (?: (?&hextet) : ){2} :: (?: ) ) (?<ipv6_4> ) (?<ipv6_5> ) (?<ipv6_6> ) (?<ipv6_7> ) (?<ipv6_8> ) (?<hextet> [[:xdigit:]]{1,4} ) (?<port_number> (?&_65530_65535) | (?&_65500_65529) | (?&_65000_65499) | (?&_60000_64999) | (?&_10000_59999) | (?&_1000_9999) | (?&_100_999) | (?&_0_99) ) (?<_65530_65535> 6553[0-5] ) (?<_65500_65529> 655[0-2]\d ) (?<_65000_65499> 65[0-4]\d{2} ) (?<_60000_64999> 6[0-4]\d{3} ) (?<_10000_59999> [1-5]\d{4} ) (?<_1000_9999> [1-9]\d{3} ) (?<_100_999> [1-9]\d{2} ) (?<pathSequence> (?: (?&pathSegment) \/ )* (?&pathSegment)? ) (?<pathSegment> (?: (?&uriUnreserved) | [:@!$&'()*+,;=] )* ) #(?<pathSegment> (?: (?&uriUnreserved) | [:@] )* ) (?<_query> (?: (?&uriUnreserved) | [:@!$&'()*+,;=?\/] )* ) (?<_fragment> (?: (?&uriUnreserved) | [:@!$&'()*+,;=?\/] )* ) )
/
gmx
Open regex in editor

Description

no description available

Submitted by github.com/Dibasic