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
No Match

r"
"
g

Test String

Code Generator

Generated Code

#include <StringConstants.au3> ; to declare the Constants of StringRegExp #include <Array.au3> ; UDF needed for _ArrayDisplay and _ArrayConcatenate Local $sRegex = "\b(?:if|for|while|else|else\s+?if|switch)\b" Local $sString = "#include "main.h"" & @CRLF & _ "#include "chassis.h"" & @CRLF & _ "#include <cmath>" & @CRLF & _ "#include <iostream>" & @CRLF & _ "" & @CRLF & _ "//Exponential Drive Function" & @CRLF & _ "int exponential (int joystickVal, float driveExp, int joyDead, int motorMin) { //e" & @CRLF & _ " int joySign;" & @CRLF & _ " int joyMax = 128 - joyDead;" & @CRLF & _ " int joyLive = abs(joystickVal) - joyDead;" & @CRLF & _ " if (joystickVal > 0) {joySign = 1;}" & @CRLF & _ " else if (joystickVal < 0) {joySign = -1;}" & @CRLF & _ " else {joySign = 0;}" & @CRLF & _ " int power = joySign * (motorMin + ((127 - motorMin) * (pow(joyLive, driveExp) / pow(joyMax, driveExp))));" & @CRLF & _ " return power;" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "void Chassis::OP_Chassis(void){" & @CRLF & _ " int Ch3 = exponential(master.get_analog(E_CONTROLLER_ANALOG_LEFT_X), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);" & @CRLF & _ " int Ch1 = exponential(master.get_analog(E_CONTROLLER_ANALOG_RIGHT_X), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);" & @CRLF & _ " int Ch4 = exponential(master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);" & @CRLF & _ " /*BaseFL.move(-Ch3 - Ch1);" & @CRLF & _ " BaseBL.move(-Ch3 - Ch1);" & @CRLF & _ " BaseFR.move(Ch3 - Ch1);" & @CRLF & _ " BaseBR.move(Ch3 - Ch1);*/" & @CRLF & _ "" & @CRLF & _ " BaseFL.move(-Ch3 - Ch1 - Ch4);" & @CRLF & _ " BaseFR.move(-Ch3 - Ch1 + Ch4);" & @CRLF & _ " BaseBL.move(Ch3 - Ch1 - Ch4);" & @CRLF & _ " BaseBR.move(Ch3 - Ch1 + Ch4);" & @CRLF & _ "" & @CRLF & _ " /*" & @CRLF & _ " ChassisMotors[BASE_FL_PORT].move(-Ch3 - Ch1 - Ch4);" & @CRLF & _ " ChassisMotors[BASE_FR_PORT].move(-Ch3 - Ch1 + Ch4);" & @CRLF & _ " ChassisMotors[BASE_BL_PORT].move(Ch3 - Ch1 - Ch4);" & @CRLF & _ " ChassisMotors[BASE_BR_PORT].move(Ch3 - Ch1 + Ch4);" & @CRLF & _ " */" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "void Chassis::stopDriving(void){" & @CRLF & _ " BaseFL.move(0);" & @CRLF & _ " BaseFR.move(0);" & @CRLF & _ " BaseBL.move(0);" & @CRLF & _ " BaseBR.move(0);" & @CRLF & _ "}" & @CRLF & _ "" & @CRLF & _ "void Chassis::TurnDistance(int direction, int targetValue, int timeout){" & @CRLF & _ "" & @CRLF & _ " float driveKP = 0.7;" & @CRLF & _ " float driveKD = 1.2;" & @CRLF & _ " //same PID Logic as turnValue" & @CRLF & _ " int motorPower;" & @CRLF & _ " int startTime = millis();" & @CRLF & _ " int currentValue = 0;" & @CRLF & _ " int err = 0;" & @CRLF & _ " int derr = 0;" & @CRLF & _ " int err_last = 0;" & @CRLF & _ " int err_sum = 0;" & @CRLF & _ " float KI = 0;" & @CRLF & _ " float p;" & @CRLF & _ " float i = 0;" & @CRLF & _ " float d;" & @CRLF & _ " direction == DIRECTION_LEFT ? BaseFL.tare_position() : BaseFR.tare_position(); //resets left encoder if turning left, right if turning right" & @CRLF & _ " while((millis() - startTime) < timeout){" & @CRLF & _ "" & @CRLF & _ " currentValue = abs(direction == DIRECTION_LEFT ? BaseFL.get_position() : BaseFR.get_position()); //gets left encoder if turning left, right if turning right" & @CRLF & _ " err = targetValue - currentValue;" & @CRLF & _ " err_last = err;" & @CRLF & _ " derr = (err - err_last);" & @CRLF & _ " p = (driveKP * err);" & @CRLF & _ " err_sum += err;" & @CRLF & _ " d = driveKD * derr;" & @CRLF & _ " motorPower = p+i+d;" & @CRLF & _ "" & @CRLF & _ " /* if(motorPower > 90){motorPower = 90;} //cap speed at 90" & @CRLF & _ " if(motorPower < -90){motorPower = -90;} */" & @CRLF & _ " motorPower = (motorPower > 90 ? 90 : motorPower < -90 ? -90 : motorPower); //cap speed at +- 90 BUG : Needs test" & @CRLF & _ " BaseFL.move(direction*motorPower);" & @CRLF & _ " BaseFR.move((direction*motorPower));" & @CRLF & _ " BaseBL.move((direction*motorPower));" & @CRLF & _ " BaseBR.move(direction*motorPower);" & @CRLF & _ "" & @CRLF & _ " delay(20);" & @CRLF & _ "" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "void Chassis::MoveDistance(int direction, int targetValue, int timeout, int cap){" & @CRLF & _ "" & @CRLF & _ " float driveKP = 0.7;" & @CRLF & _ " float driveKD = 1.2;" & @CRLF & _ " //same PID Logic as turnValue" & @CRLF & _ " int motorPower;" & @CRLF & _ " int startTime = millis();" & @CRLF & _ " int currentValue = 0;" & @CRLF & _ " int err = 0;" & @CRLF & _ " int derr = 0;" & @CRLF & _ " int err_last = 0;" & @CRLF & _ " int err_sum = 0;" & @CRLF & _ " float KI = 0;" & @CRLF & _ " float p;" & @CRLF & _ " float i = 0;" & @CRLF & _ " float d;" & @CRLF & _ " BaseFL.tare_position();" & @CRLF & _ " while((millis() - startTime) < timeout){" & @CRLF & _ "" & @CRLF & _ " currentValue = abs(BaseFL.get_position());" & @CRLF & _ " err = targetValue - currentValue;" & @CRLF & _ " err_last = err;" & @CRLF & _ " derr = (err - err_last);" & @CRLF & _ " p = (driveKP * err);" & @CRLF & _ " err_sum += err;" & @CRLF & _ " d = driveKD * derr;" & @CRLF & _ "" & @CRLF & _ " motorPower = p+i+d;" & @CRLF & _ "" & @CRLF & _ " if(motorPower > cap){motorPower = cap;}" & @CRLF & _ " if(motorPower < -cap){motorPower = -cap;}" & @CRLF & _ " // motorPower = (motorPower > 1 ? 1 : motorPower < -1 ? -1 : motorPower);" & @CRLF & _ " BaseFL.move(-direction*motorPower);" & @CRLF & _ " BaseFR.move((direction*motorPower));" & @CRLF & _ " BaseBL.move((-direction*motorPower));" & @CRLF & _ " BaseBR.move(direction*motorPower);" & @CRLF & _ "" & @CRLF & _ " delay(20);" & @CRLF & _ "" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "void Chassis::TurnGyro(int direction, int targetValue, int timeout){" & @CRLF & _ "" & @CRLF & _ " float driveKP = 1.2;" & @CRLF & _ " float driveKD = 0.8;" & @CRLF & _ "" & @CRLF & _ " gyro.reset();" & @CRLF & _ " //initialize variables" & @CRLF & _ " int motorPower; //motor power level" & @CRLF & _ " int startTime = millis(); //Elapsed time since start of the sequence" & @CRLF & _ " int currentValue = 0; //starting value of 0" & @CRLF & _ " int turn_err = 0; //error value init" & @CRLF & _ " int derr = 0;//error difference" & @CRLF & _ " int err_last = 0; //last error" & @CRLF & _ " int err_sum = 0; //sum of errors" & @CRLF & _ " float KI = 0; //KI value - not currently used'" & @CRLF & _ " float p; //p value normally 0.8" & @CRLF & _ " float i = 0; //I value" & @CRLF & _ " float d; //d value normally 0.7" & @CRLF & _ "" & @CRLF & _ " while((millis() - startTime) < timeout){" & @CRLF & _ " currentValue = gyro.get_value();" & @CRLF & _ " turn_err = targetValue - currentValue; //error is how far the current position is from the position you put into the loop" & @CRLF & _ " err_last = turn_err; //stores the error" & @CRLF & _ " derr = (turn_err - err_last); //difference between how far you were from the target last sequence compared to this sequence" & @CRLF & _ " p = (driveKP * turn_err); //p value is preset driveKP multiplied by how far we are from our target" & @CRLF & _ " err_sum += turn_err; //err_sum increases by the sum of errors" & @CRLF & _ " d = driveKD * derr; //d value is preset driveKD multiplied by the difference between how far you were from the last rotation" & @CRLF & _ "" & @CRLF & _ " motorPower = p+i+d; //motorpower is the sum of p, i, and d" & @CRLF & _ "" & @CRLF & _ " if(motorPower > 90){motorPower = 90;} //if the motor power is greater than 127 (the maximun it can go), set it to 127" & @CRLF & _ " if(motorPower < -90){motorPower = -90;}//if the motor power is less than -127 (the minimum it can go), set it to -127" & @CRLF & _ "" & @CRLF & _ " /*" & @CRLF & _ " * Move motors the motorpower value times the direction." & @CRLF & _ " * Here, the Front Left motor and the Back Left motor are moving backwards if direction == 1" & @CRLF & _ " * and the Back Right and Front Right motors are moving forwards if direction ==1" & @CRLF & _ " * this is the setup that allows the base to turn" & @CRLF & _ " */" & @CRLF & _ " BaseFL.move(direction*motorPower);" & @CRLF & _ " BaseBL.move(direction*motorPower);" & @CRLF & _ " BaseBR.move((direction*motorPower));" & @CRLF & _ " BaseFR.move((direction*motorPower));" & @CRLF & _ "" & @CRLF & _ " delay(20);" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ "}" & @CRLF & _ "void Chassis::StrafeDistance(int direction, int targetValue, int timeout){" & @CRLF & _ "" & @CRLF & _ " float driveKP = 0.7;" & @CRLF & _ " float driveKD = 1.2;" & @CRLF & _ " //same PID Logic as turnValue" & @CRLF & _ " int motorPower;" & @CRLF & _ " int startTime = millis();" & @CRLF & _ " int currentValue = 0;" & @CRLF & _ " int err = 0;" & @CRLF & _ " int derr = 0;" & @CRLF & _ " int err_last = 0;" & @CRLF & _ " int err_sum = 0;" & @CRLF & _ " float KI = 0;" & @CRLF & _ " float p;" & @CRLF & _ " float i = 0;" & @CRLF & _ " float d;" & @CRLF & _ " direction == DIRECTION_LEFT ? BaseFL.tare_position() : BaseFR.tare_position(); //resets left encoder if turning left, right if turning right" & @CRLF & _ " while((millis() - startTime) < timeout){" & @CRLF & _ "" & @CRLF & _ " currentValue = abs(direction == DIRECTION_LEFT ? BaseFL.get_position() : BaseFR.get_position()); //gets left encoder if turning left, right if turning right" & @CRLF & _ " err = targetValue - currentValue;" & @CRLF & _ " err_last = err;" & @CRLF & _ " derr = (err - err_last);" & @CRLF & _ " p = (driveKP * err);" & @CRLF & _ " err_sum += err;" & @CRLF & _ " d = driveKD * derr;" & @CRLF & _ "" & @CRLF & _ " motorPower = p+i+d;" & @CRLF & _ "" & @CRLF & _ " /* if(motorPower > 90){motorPower = 90;} //cap speed at 90" & @CRLF & _ " if(motorPower < -90){motorPower = -90;} */" & @CRLF & _ " motorPower = (motorPower > 90 ? 90 : motorPower < -90 ? -90 : motorPower); //cap speed at +- 90 BUG : Needs test" & @CRLF & _ " BaseFL.move(direction*motorPower);" & @CRLF & _ " BaseFR.move((direction*motorPower));" & @CRLF & _ " BaseBL.move(-(direction*motorPower));" & @CRLF & _ " BaseBR.move(-direction*motorPower);" & @CRLF & _ "" & @CRLF & _ " delay(20);" & @CRLF & _ "" & @CRLF & _ " }" & @CRLF & _ "}" & @CRLF & _ "void Chassis::driveOnAngle(double angle, int quadrant, int targetValue, int timeout){" & @CRLF & _ "" & @CRLF & _ " //PD Constants" & @CRLF & _ " float driveKP = 0.7;" & @CRLF & _ " float driveKD = 1.2;" & @CRLF & _ "" & @CRLF & _ " //General variables" & @CRLF & _ " int motorPower;" & @CRLF & _ " int startTime = millis();" & @CRLF & _ " int currentValue = 0;" & @CRLF & _ " int err = 0;" & @CRLF & _ " int derr = 0;" & @CRLF & _ " int err_last = 0;" & @CRLF & _ " int err_sum = 0;" & @CRLF & _ " float KI = 0;" & @CRLF & _ " float p = 0;" & @CRLF & _ " float i = 0;" & @CRLF & _ " float d = 0;" & @CRLF & _ "" & @CRLF & _ " //Wheel modification values" & @CRLF & _ " double FRMod;" & @CRLF & _ " double FLMod;" & @CRLF & _ " double BLMod;" & @CRLF & _ " double BRMod;" & @CRLF & _ "" & @CRLF & _ " BaseFL.tare_position();" & @CRLF & _ " BaseFR.tare_position();" & @CRLF & _ " BaseBL.tare_position();" & @CRLF & _ " BaseBR.tare_position();" & @CRLF & _ " //Convert degree to radians" & @CRLF & _ " // angle = angle * (M_PI / 180);" & @CRLF & _ "" & @CRLF & _ " //Determine which qaudrant we're driving in" & @CRLF & _ " /*" & @CRLF & _ " if (angle < (M_PI / 2) && angle > 0 ) {" & @CRLF & _ " quadrant = QUADRANT_1;" & @CRLF & _ " }" & @CRLF & _ " else if(angle < (M_PI) && angle > (M_PI / 2)) {" & @CRLF & _ " quadrant = QUADRANT_2;" & @CRLF & _ " }" & @CRLF & _ " else if((angle < ((3*M_PI) / 2)) && angle > (M_PI)) {" & @CRLF & _ " quadrant = QUADRANT_3;" & @CRLF & _ " }" & @CRLF & _ " else if(angle < (2*M_PI) && angle > (((3*M_PI) / 2))){" & @CRLF & _ " quadrant = QUADRANT_4;" & @CRLF & _ " }" & @CRLF & _ "*/" & @CRLF & _ " while((millis() - startTime) < timeout){" & @CRLF & _ "" & @CRLF & _ " switch (quadrant) { //Change wheel modifications and enccder values based on the quadrant we're driving in." & @CRLF & _ "" & @CRLF & _ " case QUADRANT_1 : //If we're driving in quadrant 1 (0 to π/2)" & @CRLF & _ " currentValue = abs(BaseFR.get_position());" & @CRLF & _ " FRMod = sin(angle - (M_PI/4));" & @CRLF & _ " FLMod = -1;" & @CRLF & _ " BLMod = -sin(angle - (M_PI / 4));" & @CRLF & _ " BRMod = 1;" & @CRLF & _ "" & @CRLF & _ " case QUADRANT_2 : //If we're driving in quadrant 1 (π/2 to π)" & @CRLF & _ " currentValue = abs(BaseFL.get_position());" & @CRLF & _ " FLMod = 1;" & @CRLF & _ " FRMod = sin(angle - ((3*M_PI)/4));" & @CRLF & _ " BRMod = 1;" & @CRLF & _ " BLMod = sin(angle - ((3*M_PI)/4));" & @CRLF & _ "" & @CRLF & _ " case QUADRANT_3 : //BUG FL, BR might be Negative as well" & @CRLF & _ " currentValue = abs(BaseBL.get_position());" & @CRLF & _ " FLMod = 1;" & @CRLF & _ " BLMod = -(sin(angle - (M_PI)/4));" & @CRLF & _ " BRMod = -1;" & @CRLF & _ " FRMod = (sin (angle - ((M_PI)/4)));" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ " case QUADRANT_4:" & @CRLF & _ " /*BACK LEFT ALIGNMENT*/" & @CRLF & _ " currentValue = abs(BaseBL.get_position());" & @CRLF & _ " FLMod = 1;" & @CRLF & _ " BLMod = -(sin(angle - (M_PI)/4));" & @CRLF & _ " BRMod = -1;" & @CRLF & _ " FRMod = (sin (angle - ((M_PI)/4)));" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ " err = targetValue - currentValue; //error is how far the current position is from the position you put into the loop" & @CRLF & _ " err_last = err; //stores the error" & @CRLF & _ " derr = (err - err_last); //difference between how far you were from the target last sequence compared to this sequence" & @CRLF & _ " p = (driveKP * err); //p value is preset driveKP multiplied by how far we are from our target" & @CRLF & _ " err_sum += err; //err_sum increases by the sum of errors" & @CRLF & _ " d = driveKD * derr; //d value is preset driveKD multiplied by the difference between how far you were from the last rotation" & @CRLF & _ "" & @CRLF & _ " motorPower = p+i+d; //motorpower is the sum of p, i, and d" & @CRLF & _ "" & @CRLF & _ " if(motorPower > 90){motorPower = 90;} //if the motor power is greater than 127 (the maximun it can go), set it to 127" & @CRLF & _ " if(motorPower < -90){motorPower = -90;}//if the motor power is less than -127 (the minimum it can go), set it to -127" & @CRLF & _ "" & @CRLF & _ " //moves the wheels according to the PID output and the Quadrant Modifications" & @CRLF & _ " BaseFL.move(motorPower * FLMod);" & @CRLF & _ " BaseBL.move(motorPower * BLMod);" & @CRLF & _ " BaseBR.move(motorPower * BRMod);" & @CRLF & _ " BaseFR.move(motorPower * FRMod);" & @CRLF & _ "" & @CRLF & _ " delay(20);" & @CRLF & _ " }" & @CRLF & _ "" & @CRLF & _ "}" Local $aArray = StringRegExp($sString, $sRegex, $STR_REGEXPARRAYGLOBALFULLMATCH) Local $aFullArray[0] For $i = 0 To UBound($aArray) -1 _ArrayConcatenate($aFullArray, $aArray[$i]) Next $aArray = $aFullArray ; Present the entire match result _ArrayDisplay($aArray, "Result")

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 AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm