Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
mg

Test String

Code Generator

Generated Code

$re = '/User name\s+(?<user_name>[a-zA-Z|\.|\-|\_|0-9]+)\nFull Name\s+(?<full_name>[a-zA-Z|\.|\-|\_|0-9| ]*)\nComment\s+(?<comment>[a-zA-Z|\.|\-|\_|0-9| ]+)\nUser\'s comment\s+(?<user_comment>[a-zA-Z|\.|\-|\_|0-9| ]+)\nCountry\/region code\s+(?<country_region_code>[a-zA-Z|\.|\-|\_|0-9| |\(|\)]+)\nAccount active\s+(?<account_active>[No|Yes]+)\nAccount expires\s+(?<account_expiring_date_and_time>Never|(?<account_expiring_date>(?<account_expiring_date_month>\d{1,2})\/(?<account_expiring_date_day>\d{1,2})\/(?<account_expiring_date_year>\d{4}))\s(?<account_expiring_time>(?<account_expiring_time_hours>\d{1,2})\:(?<account_expiring_time_mins>\d{1,2})\:(?<account_expiring_time_seconds>\d{1,2})\s(?<account_expiring_time_am_pm>AM|PM)))\n{2}Password last set\s+(?<password_last_set_date_and_time>Never|(?<password_last_set_date>(?<password_last_set_date_month>\d{1,2})\/(?<password_last_set_date_day>\d{1,2})\/(?<password_last_set_date_year>\d{4}))\s(?<password_last_set_time>(?<password_last_set_time_hours>\d{1,2})\:(?<password_last_set_time_mins>\d{1,2})\:(?<password_last_set_time_seconds>\d{1,2})\s(?<password_last_set_time_am_pm>AM|PM)))\nPassword expires\s+(?<password_expires_date_and_time>Never|(?<password_expires__date>(?<password_expires_date_month>\d{1,2})\/(?<password_expires_date_day>\d{1,2})\/(?<password_expires_date_year>\d{4}))\s(?<password_expires_time>(?<password_expires_time_hours>\d{1,2})\:(?<password_expires_time_mins>\d{1,2})\:(?<password_expires_time_seconds>\d{1,2})\s(?<password_expires_time_am_pm>AM|PM)))\nPassword changeable\s+(?<password_change_date_and_time>Never|(?<password_change_date>(?<password_change_date_month>\d{1,2})\/(?<password_change_date_day>\d{1,2})\/(?<password_change_date_year>\d{4}))\s(?<password_change_time>(?<password_change_time_hours>\d{1,2})\:(?<password_change_time_mins>\d{1,2})\:(?<password_changes_time_seconds>\d{1,2})\s(?<password_change_time_am_pm>AM|PM)))\nPassword required\s+(?<account_required>[Yes|No]+)\nUser\smay change password\s+(?<user_may_change_password>[No|Yes]+)\n{2}Workstations allowed\s+(?<workstations_allowed>.*)\nLogon script\s+(?<logon_script>.*)\nUser profile\s+(?<user_profile>.*)\nHome directory\s+(?<home_directory>.*)\nLast logon\s+(?<last_logon>(?<last_logon_date_and_time>Never|(?<last_logon_date>(?<last_logon_date_month>\d{1,2})\/(?<last_logon_date_day>\d{1,2})\/(?<last_logon_date_year>\d{4}))\s(?<last_logon_time>(?<last_logon_time_hours>\d{1,2})\:(?<last_logon_time_mins>\d{1,2})\:(?<last_logon_time_seconds>\d{1,2})\s(?<last_logon_time_am_pm>AM|PM))))\n{2}Logon hours allowed\s+(((?<day>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon>(?<starting_allowed_logon_hours>\d{1,2})\:(?<starting_allowed_logon_mins>\d{1,2})\:(?<starting_allowed_logon_seconds>\d{1,2})\s(?<starting_allowed_logon_am_pm>[AM|PM]+))\s\-\s(?<endding_allowed_logon>(?<ending_allowed_logon_hours>\d{1,2})\:(?<ending_allowed_logon_mins>\d{1,2})\:(?<ending_allowed_logon_seconds>\d{1,2})\s(?<ending_allowed_logon_am_pm>[AM|PM]+))?\n?\s+?)?((?<day1>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon1>(?<starting_allowed_logon_hours1>\d{1,2})\:(?<starting_allowed_logon_mins1>\d{1,2})\:(?<starting_allowed_logon_seconds1>\d{1,2})\s(?<starting_allowed_logon_am_pm1>[AM|PM]+))\s\-\s(?<endding_allowed_logon1>(?<ending_allowed_logon_hours1>\d{1,2})\:(?<ending_allowed_logon_mins1>\d{1,2})\:(?<ending_allowed_logon_seconds1>\d{1,2})\s(?<ending_allowed_logon_am_pm1>[AM|PM]+))?\n?\s+?)?((?<day2>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon2>(?<starting_allowed_logon_hours2>\d{1,2})\:(?<starting_allowed_logon_mins2>\d{1,2})\:(?<starting_allowed_logon_seconds2>\d{1,2})\s(?<starting_allowed_logon_am_pm2>[AM|PM]+))\s\-\s(?<endding_allowed_logon2>(?<ending_allowed_logon_hours2>\d{1,2})\:(?<ending_allowed_logon_mins2>\d{1,2})\:(?<ending_allowed_logon_seconds2>\d{1,2})\s(?<ending_allowed_logon_am_pm2>[AM|PM]+))?\n?\s+?)?((?<day3>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon3>(?<starting_allowed_logon_hours3>\d{1,2})\:(?<starting_allowed_logon_mins3>\d{1,2})\:(?<starting_allowed_logon_seconds3>\d{1,2})\s(?<starting_allowed_logon_am_pm3>[AM|PM]+))\s\-\s(?<endding_allowed_logon3>(?<ending_allowed_logon_hours3>\d{1,2})\:(?<ending_allowed_logon_mins3>\d{1,2})\:(?<ending_allowed_logon_seconds3>\d{1,2})\s(?<ending_allowed_logon_am_pm3>[AM|PM]+))?\n?\s+?)?((?<day4>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon4>(?<starting_allowed_logon_hours4>\d{1,2})\:(?<starting_allowed_logon_mins4>\d{1,2})\:(?<starting_allowed_logon_seconds4>\d{1,2})\s(?<starting_allowed_logon_am_pm4>[AM|PM]+))\s\-\s(?<endding_allowed_logon4>(?<ending_allowed_logon_hours4>\d{1,2})\:(?<ending_allowed_logon_mins4>\d{1,2})\:(?<ending_allowed_logon_seconds4>\d{1,2})\s(?<ending_allowed_logon_am_pm4>[AM|PM]+))?\n?\s+?)?((?<day5>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon5>(?<starting_allowed_logon_hours5>\d{1,2})\:(?<starting_allowed_logon_mins5>\d{1,2})\:(?<starting_allowed_logon_seconds5>\d{1,2})\s(?<starting_allowed_logon_am_pm5>[AM|PM]+))\s\-\s(?<endding_allowed_logon5>(?<ending_allowed_logon_hours5>\d{1,2})\:(?<ending_allowed_logon_mins5>\d{1,2})\:(?<ending_allowed_logon_seconds5>\d{1,2})\s(?<ending_allowed_logon_am_pm5>[AM|PM]+))?\n?\s+?)?((?<day6>[Friday|Saturday|Sunday|Monday|Tuesday|Wednesday|Thursday]+)\s(?<starting_allowed_logon6>(?<starting_allowed_logon_hours6>\d{1,2})\:(?<starting_allowed_logon_mins6>\d{1,2})\:(?<starting_allowed_logon_seconds6>\d{1,2})\s(?<starting_allowed_logon_am_pm6>[AM|PM]+))\s\-\s(?<endding_allowed_logon6>(?<ending_allowed_logon_hours6>\d{1,2})\:(?<ending_allowed_logon_mins6>\d{1,2})\:(?<ending_allowed_logon_seconds6>\d{1,2})\s(?<ending_allowed_logon_am_pm6>[AM|PM]+))?\n?\s+?)?)\nLocal Group Memberships\s+(((?:\*)(?<local_group_memberships>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships1>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships2>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships3>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships4>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships5>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships6>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships7>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships8>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships9>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<local_group_memberships10>\b[\s|\w]+\b)[\s|\n]*)?)?Global Group memberships\s+(((?:\*)(?<global_group_memberships>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships1>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships2>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships3>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships4>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships5>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships6>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships7>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships8>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships9>\b[\s|\w]+\b)[\s|\n]*)?((?:\*)(?<global_group_memberships10>\b[\s|\w]+\b)[\s|\n]*)?)The command completed successfully\./m'; $str = 'User name x.xxxxxx Full Name Xxx X. Xxxxxxx Comment Xxxxx Xxxxxx Xxxxx User\'s comment Xxxxxxxxxxxxxx Country/region code (null) Account active Yes Account expires 7/7/2020 12:00:00 AM Password last set 6/6/2020 4:04:07 PM Password expires 7/18/2020 4:04:07 PM Password changeable 6/7/2020 4:04:07 PM Password required Yes User may change password Yes Workstations allowed All Logon script xxxx.xxxx User profile c:\\Users\\xxxxxxxx\\Desktop\\script.ps1 Home directory c:\\Users\\xxxxxxxx\\Desktop\\script.ps1 Last logon Never Logon hours allowed Sunday 8:00:00 AM - 4:00:00 PM Monday 8:00:00 AM - 4:00:00 PM Tuesday 8:00:00 AM - 4:00:00 PM Wednesday 8:00:00 AM - 4:00:00 PM Thursday 8:00:00 AM - 4:00:00 PM Local Group Memberships *Access Control Assist*Administrators *Allowed RODC Password*Cert Publishers Global Group memberships *Domain Users The command completed successfully. '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php