Regular Expressions 101

Community Patterns

21

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·2023-07-09 07:25
Flavor·PCRE2 (PHP)

/
^(([5,6,7,8,9]{1})([0-9]{1})([0,1,2,3,5,6,7,8]{1})([0-9]{6})([v|V|x|X]))|(([1,2]{1})([0,9]{1})([0-9]{2})([0,1,2,3,5,6,7,8]{1})([0-9]{7}))
/
gm
Open regex in editor

Description

Extract the Data from Sri Lankan National Identity Card

The SL NIC could be divided into two groups,

  1. Before 2016 (9 alphanumeric digits with one English letter)
  2. After 2016 (12 alphanumeric digits)

NICs issued before 1 January 2016, each NIC has a unique 9-digit number and a letter, in the format 000000000V (where 0 is a digit and V is a letter). The first two digits of the number are the holder's year of birth (e.g.: 91xxxxxxxx for someone born in 1991). The next three digits contain the number of the day in the year for the person's birth. For females, 500 is added to the number of days. The next three digits number is the serial number of the issued day. The next digit is the check digit. The final letter is generally a 'V' which indicates that the holder is eligible to vote in the area. In some cases the final letter can be 'X' which usually indicates the holder is not eligible to vote; possibly because they were not permanent residents of Sri Lanka when applying for a NIC. (Directly copied from Wikipedia)

  1. Old NIC - Before 2016

Birth Year: 72 Day Count for Birthday: 244 Serial Number; 152 Check Digit: 4 Special Letter: V

The NIC Number: 722441524V

  1. New NIC - After 2016

Birth Year: 2001 Day Count for Birthday: 253 Serial Number; 0297 Check Digit: 6 Special Letter: -

The NIC Number: 200125302976

  • Can be used for any men or women born in 1950-2999
Submitted by Aadhil Ahamed (Find me on GitHub: https://github.com/AadhilMR)