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

1

Regular Expression
Created·2020-11-20 19:31
Flavor·JavaScript

/
(?<=\D|^)(?<year>\d{4})(?<sep>[^\w\s])(?<month>1[0-2]|0[1-9])\k<sep>(?<day>0[1-9]|[12][0-9]|(?<=11\k<sep>|[^1][4-9]\k<sep>)30|(?<=1[02]\k<sep>|[^1][13578]\k<sep>)3[01])(?=\D|$)
/
gm
Open regex in editor

Description

A fully tested regex that extracts and validates date parts using named capturing groups.


Validations:

  • Year must be preceded by nothing or a non-digit character
  • Year must have 4 digits
  • Month must be between 01 and 12
  • Month must have 2 digits
  • Day must be between 01 and the maximum number of days for the month (e.g. february can't have more than 29 days)
  • Day must have 2 digits
  • Day must be followed by nothing or a non-digit character
  • Separator must be any single character that is not a space or an alphanumeric character
  • Separator must be the same between each date part


Capturing groups:

| # |   Name  | Description                         |
|:-:|:-------:|-------------------------------------|
| 1 |  `year` | 4 digits of the year                |
| 2 |  `sep`  | Date parts separator                |
| 3 | `month` | 2 digits of the month               |
| 4 |  `day`  | 2 digits of the date (day of month) |


Example usage:

let match = regex.exec('2020-11-22')

console.log('year: %s, month: %s, day: %s',
            match.groups.year,
            match.groups.month,
            match.groups.day)

// year: 2020, month: 11, day: 22


Compatibility: (updated 2020-11-20)

  • Chrome >= 64
  • Edge >= 79
  • Firefox >= 78
  • IE incompatible (lookbehind assertions & named capture groups not supported)
  • Opera >= 51
  • Safari incompatible (lookbehind assertions not supported)
  • NodeJS >= 10.0.0

See regex compatibility table.


Note: does not validate leap years (not really possible in regex)

Submitted by Elie Grenon (DrunkenPoney) <elie.grenon.1@gmail.com>