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-11-13 07:46
Flavor·PCRE (Legacy)

/
(?############# ### AS PHP ### ##############) (?##################################################### ### DECLEAR GROUPS CAPTURE NAME WITHOUT USING THEM ### #######################################################) (?(DEFINE) (?<_VAR_NAME>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*) (?# Valid variable name) (?<_BOOLEAN>(?i:false|true)) (?# Valid boolean value) (?<_STRING>(?:"(?:[\\\\]+.|[^"])*")|(?:\'(?:[\\\\]+.|[^\'])*\')) (?# Valid string value) (?<_INT>[0-9]+) (?# Valid integer value) (?<__INT>(?:(?:-|\+)\s*)?(?P>_INT)) (?# Valid positive or negative interger value) (?<_KEY>(?P>_STRING)|(?P>_INT)|(?P>_CALL)) (?# Valid key name) (?<__KEY>(?:\[(?P>_KEY)\])+) (?# Valid multiple keys) (?<_CALL>\$(?P>_VAR_NAME)(?P>__KEY)?) (?# Valid variable calls) (?<_VALUE>(?i:null)|(?P>_BOOLEAN)|(?P>_STRING)|(?P>__INT)|(?P>_CALL)) (?# Valid value without array value) (?<_ARRAY>\[\s*(?:(?:(?:(?P>_VALUE)|(?P>_ARRAY))\s*,\s*)*(?:(?:(?P>_VALUE)|(?P>_ARRAY))\s*))?\])(?# Valid array) (?<__VALUE>(?:(?P>_VALUE)|(?P>_ARRAY))) (?# Valid value with array value) (?<_OPERATORS>={2,3}|!={1,2}|>=?|<=?) (?# Valid operators signs) (?<_SHORT_OPR>AND|OR|and|or|&{2}|\|{2}) (?# Valid short-handed opreators signs) ) (?##################### ### START CAPTURE ### #######################) (?# Get the full match capture text) (?:<!--(?s:.)+?-->|<!--(?s:.)*)(*SKIP)(*FAIL)| (?# SKIP ON COMMENTS FOUND) (?#[^\\\\](?:[\\\\]{2})(?#*[\\\\](?P>text)(?#(*SKIP)(?#(*FAIL)(?#| (?# SKIP ESCAPED MATCHES) (?<text> (?# Find new variables declare with multiple keys - mark as SET) (*:SET) (?<target>\$(?<name>(?P>_VAR_NAME))(?<key>(?P>__KEY))?) (?:(?<A>)|(?<B>\s*)) (?<operator> (?(B) (?:(?<C>\+=|-=)|(?<D>\.=|=\.)) |(?(A) (?<E>\+{2}|\-{2})) ) ) (?(E)|(?:\s*(?<value> (?(B)(?P>__VALUE)| (?(C)(?:(?P>__INT)|(?P>_CALL))| (?(D)(?:(?P>_STRING)|(?P>_CALL))) ) ) ))); (?########## OR ##########)| (?# Find new variables declare with multiple keys - mark as SET) (*:SET) (?<target>\$(?<name>(?P>_VAR_NAME))(?<key>(?P>__KEY))?)\s*=\s*(?<value>(?P>__VALUE)); (?########## OR ##########)| (?# Find variable calls with multiple keys - mark as CALL) (*:CALL) (?<target>\$(?<name>(?P>_VAR_NAME))(?<key>(?P>__KEY))?) (?########## OR ##########)| (?# Find conditional statements - mark as IF) (*:IF) (?i)(?:if)(?-i)\s* \((?<statements>(?P>_VALUE)(?:\s*(?P>_OPERATORS)\s*(?P>_VALUE)(?!(?P>_OPERATORS))|(?:\s+(?P>_SHORT_OPR)\s+(?P>_VALUE))*)+)\)\s*{ (?<content>(?:|(?R)|.|\s)*) } (?########## OR ##########)| (?# Find while loops - mark as WHILE) (*:WHILE) (?i)(?:while)(?-i)\s* \((?<statements>(?P>_VALUE)(?:\s*(?P>_OPERATORS)\s*(?P>_VALUE)(?!(?P>_OPERATORS))|(?:\s+(?P>_SHORT_OPR)\s+(?P>_VALUE))*)+)\)\s*{ (?<content>(?:|(?R)|.|\s)*) } )
/
gmxJ
Open regex in editor

Description

Almost ready!!!

Submitted by anonymous