PHP Classes

PHP Regex Advanced: Match MSDOS/UNIX patterns with regular expressions

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 74%Total: 362 All time: 6,866 This week: 113Up
Version License PHP version Categories
advanced-regex 1.0.8BSD License5.5PHP 5, Text processing
Description 

Author

This class can match MSDOS or UNIX patterns with regular expressions among other features. Currently it can:

- Take a given pattern in MSDOS or UNIX format and convert it to regular expressions determine if a given string matches that pattern.
- Take a list of regular expressions and string to evaluate and return string that identifies the sequence of regular expressions that the input string matches.
- Take a list of strings and develops a regular expression that would match those strings.
- Use regular expressions that can specify several times the same named capture groups
- Meta-matching groups of lines based on a set of supplied regular expressions. Meta-matching is very powerful since a single regular expression can be used to match groups of lines in a file (each line being itself described by its own regular expression)

Innovation Award
PHP Programming Innovation award nominee
September 2015
Number 11


Prize: One downloadable copy of PhpED Professional
Regular expressions are powerful for matching and extracting patterns of text from strings. However, sometimes it is hard to figure the right pattern to match the strings we need.

This class provides a solution to automatically create regular expressions that match a set of given strings with common patterns.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 57
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

<?php
// test Regex class methods
// this script should be run in command-line mode
include ( 'Regex.class.php' ) ;

if (
php_sapi_name ( ) == 'cli' )
    {
   
$eol = PHP_EOL ;
   
$tab = "\t" ;
     }
else
    {
   
$eol = "<br/>" ;
   
$tab = str_repeat ( "&nbsp;", 8 ) ;
     }


// IsRegex() method example
$re =
   [
   
'/foo bar/', // valid
   
'#foo bar#imsx', // valid
   
'/foo bar' // invalid
   
] ;

foreach (
$re as $item )
    echo (
"Regex::IsRegex ( $item ) = " . ( ( Regex::IsRegex ( $item ) ) ? 'true' : 'false' ) . $eol ) ;

echo (
$eol ) ;

// Matches() example
$matches =
   [
    [
'file.txt', '*.txt' ], // match
   
[ 'file.txt', 'file.*' ], // match
   
[ 'dir/file.txt', '*.txt' ], // no match
   
[ 'dir/file.txt', 'dir/*.txt' ], // match
   
[ 'dir/file.txt', '*/*.txt' ], // match
   
[ 'dir/file.txt', '*\\file.txt' ] // match
   
] ;

foreach (
$matches as $match )
    echo (
"Regex::Matches ( {$match [0]}, {$match [1]} ) = " . ( ( Regex::Matches ( $match [0], $match [1] ) ) ? 'true' : 'false' ) . $eol ) ;

echo (
$eol ) ;

// DevelopExpression() example
$expressions =
   [
   
'file[0-9].txt', // Gives 'file0.txt' to 'file9.txt'
   
'file[a-c][0-2].bin' // Gives 'filea0.bin' to 'filea2.bin', 'fileb0.bin' to 'fileb2.bin', etc.
   
] ;


foreach (
$expressions as $expression )
    {
    echo (
"Regex::DevelopExpression ( $expression ) = $eol$tab" ) ;
   
$developed_expressions = Regex::DevelopExpression ( $expression ) ;
    echo (
rtrim ( str_replace ( "\n", "$eol$tab", print_r ( $developed_expressions, true ) ) ) ) ;
    echo (
"$eol" ) ;
     }

echo (
$eol ) ;

// PregMatchEx() example
$subject = "a:1 b:2" ;
$re = '/(?P<sequence> (?P<letter> [a-z]) : (?P<digit> [0-9])) \s (?P<sequence> (?P<letter> [a-z]) : (?P<digit> [0-9]))/imsx' ;

echo (
"Regex::PregMatchEx ( $subject, $re, WIPE ) : " ) ;
$result = Regex::PregMatchEx ( $re, $subject, $match, PREG_WIPE_MATCHES | PREG_OFFSET_CAPTURE ) ;
echo (
"\t" . str_replace ( "\n", "$eol$tab", print_r ( $match, true ) ) ) ;
echo (
$eol ) ;

echo (
"Regex::PregMatchEx ( $subject, $re, NOWIPE ) : " ) ;
$result = Regex::PregMatchEx ( $re, $subject, $match, PREG_OFFSET_CAPTURE ) ;
echo (
"\t" . str_replace ( "\n", "$eol$tab", print_r ( $match, true ) ) ) ;
echo (
$eol ) ;


// MetaPregMatchEx() example
$regex_list =
   [
   
'1' => '/message start/',
   
'2' => '/log: \s* (?P<logmessage> .*)/imsx',
   
'3' => '/message end/'
   
] ;
$sequence = '/ \1 \2* \3 /imsx' ;
$lines =
   [
   
'message start',
   
'log: this is log message 1',
   
'log: this is log message 2',
   
'message end'
   
] ;

echo (
"Regex::MetaPregMatchEx ( ) = " ) ;
$status = Regex::MetaPregMatchEx ( $sequence, $regex_list, $lines ) ;
echo ( (
$status ) ? 'true' : 'false' ) ;


  Files folder image Files  
File Role Description
Accessible without login Plain text file DISCLAIMER Data Disclaimer
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License file
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login HTML file README.html Doc. Readme file
Plain text file Regex.class.php Class The Regex class
Accessible without login Plain text file Regex.md Doc. Documentation

 Version Control Reuses Unique User Downloads Download Rankings  
 71%1
Total:362
This week:0
All time:6,866
This week:113Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:74%StarStarStarStar
Rank:180