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-11-21 07:37
Flavor·PCRE2 (PHP)

Description

<?php session_start(); ?> <!DOCTYPE html> <html> <head> <title>Tier Verwalter</title> </head> <body> <?php class Tier { private $name; private $alter; private $gewicht; private $geburtsdatum; private $art; private $id; public function __construct($name = '', $alter = 0, $gewicht = 0, $geburtsdatum = '', $art = '') { $this->setName($name); $this->setAlter($alter); $this->setGewicht($gewicht); $this->setGeburtsdatum($geburtsdatum); $this->setArt($art); $this->id = uniqid(); } // Getter und Setter für name public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } // Getter und Setter für alter public function setAlter($alter) { $this->alter = $alter; } public function getAlter() { return $this->alter; } // Getter und Setter für gewicht public function setGewicht($gewicht) { $this->gewicht = $gewicht; } public function getGewicht() { return $this->gewicht; } // Getter und Setter für geburtsdatum public function setGeburtsdatum($geburtsdatum) { $this->geburtsdatum = $geburtsdatum; } public function getGeburtsdatum() { return $this->geburtsdatum; } // Getter und Setter für art public function setArt($art) { $this->art = $art; } public function getArt() { return $this->art; } public function getId() { return $this->id; } } if (isset($_POST['speichern'])) { $neuesTier = new Tier( $_POST['name'], $_POST['alter'], $_POST['gewicht'], $_POST['geburtsdatum'], $_POST['art'] ); if (!isset($_SESSION['tiere'])) { $_SESSION['tiere'] = array(); } $_SESSION['tiere'][$neuesTier->getId()] = $neuesTier; } if (isset($_POST['loeschen'])) { if (isset($_SESSION['tiere'])) { unset($_SESSION['tiere']); $_SESSION['tiere'] = array(); } } ?> <h2>Neues Tier hinzufügen</h2> <form method="POST"> <label>Name: </label> <input type="text" name="name" value="Fido" required><br><br>
<label>Alter: </label>
<input type="number" name="alter" value="3" min="0" required><br><br>

<label>Gewicht (kg): </label>
<input type="number" name="gewicht" value="15.5" step="0.1" min="0" required><br><br>

<label>Geburtsdatum: </label>
<input type="date" name="geburtsdatum" value="2022-01-15"><br><br>

<label>Art: </label>
<input type="text" name="art" value="Hund" required><br><br>

<button type="submit" name="speichern" value="speichern">Speichern</button>
<button type="submit" name="loeschen" value="loeschen">Alle löschen</button>
</form> <h3>Gespeicherte Tiere:</h3> <?php if (isset($_SESSION['tiere']) && count($_SESSION['tiere']) > 0) { foreach ($_SESSION['tiere'] as $tier) { echo "<p>Tier: " . htmlspecialchars($tier->getName()) . "</p>"; } } else { echo "<p>Keine Tiere gespeichert.</p>"; } ?> </body> </html>
Submitted by anonymous