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·2025-02-13 20:50
Flavor·PCRE2 (PHP)

/
(?i)(?=(\d)\d\d([\s\-\x{2013}\x{2014}\/]))(?<![\d\-\x{2013}\x{2014}%])(?!000|666|9\d\d|\1{3}.\1{2}.\1{4}?|012.34.5678|123.45.6789|234.56.7890|098.76.5432|876.54.3210|078.05.1120|219.09.9999)\d{3}\2(?!00)\d{2}\2(?!0000)\d{4}(?![\d\-\x{2013}\x{2014}%])(?!\.(?:pdf|docx?|xlsx?|pptx?|zip|jpe?g|png|txt|log)\b)
/
gm
Open regex in editor

Description

Developed for use with Microsoft Purview DLP, which uses the PCRE-compatible "Boost.Regex" engine for pattern matching.

Matches formatted US Social Security number patterns (i.e. nine digits with separators):

  • Allow space \s, tab \t, dash -, emdash, endash, or slash / separators.
    (Separators within an SSN all must be of the same type.)
  • Exclude all-zero area, group, or serial segment sequences:
    000-XX-XXXX, XXX-00-XXXX, or XXX-XX-0000
  • Exclude group numbers 666 and 9##:
    666-XX-XXXX or 900-XX-XXXX
  • Exclude ascending and descending number sequences:
    123-45-6789, 876-54-3210, etc.
  • Excludes known retired SSNs:
    078-05-1120 and 219-09-9999
  • Boundary checks to prevent matching on telephone, credit card, and other non-SSN types.
  • Excludes sequences ending with common file extensions:
    .pdf, .doc(x), .xls(x), .ppt(x), .zip, .jp(e)g, .png, and .log

Derived from: Comprehensive US SSN (Social Security Number) Also uses patterns from the "U.S. Social Security Number (SSN) (Nucleuz Inc)" Sensitive Information Type contained in the Microsoft Purview DLP tool.

Submitted by J. Greg Mackinnon