A preg_match() Regular Expression Tester for Your Development Server

Honk if you love regular expressions!

disclaimer
posted: 2017-05-03

Canada goose mugshot

Honk if you love regular expressions!

Here’s a simple PHP script for testing regular expressions that you can run from the comfort of your own development webserver. If you want to save your hard-won expression for future reference, click the “save it” button to add it to a text file.

Why this script instead of an online regex tester?

No advertising, no cookies, no surreptitious data collection, and, because the file sits on your development webserver, you can tweak the code until the cows come home. Oh, and it’s got pretty colours.

What do I need to run it?

You’ll need a development webserver with PHP installed. Please note, this script is not intended for and should not be used on a production webserver.

How do I set it up?

The paths and folders shown here are just examples, make sure to substitute your own.

  1. Create a folder for the script in your webserver’s document root: /your/path/to/htdocs/regex_tester. Copy the text at regex_tester.txt, paste it into a text editor, and save it as regex_tester.php in the regex_tester folder.
  2. Create a folder for the stylesheet: /your/path/to/regex_tester/css. Copy the text at regex_styles.txt, paste it into a text editor, and save it as regex_styles.css in the css folder.
  3. Running Linux or macOS? See the section on file permissions below.

Opening the file:

With your development webserver up and running, point your browser to http://localhost/regex_tester/​regex_tester.php and press Enter. The regex tester will appear in fanciful colours and ready for your regex wizardry.

Testing your expressions:

Enter any valid regular expression and a string to test. Click the “try it!” button to check for matches. The description field is only required if you are going to save your work.

File permissions for Linux/macOS:

While Windows plods along with nary a complaint, depending on your file permissions, Linux and macOS may greet you with a file error when the script tries to create or write to the file used to save your regular expressions. Both the user you log in as and the user running the Apache server process will need read/write/execute access to the regex_tester folder, its files, and subfolders therein. If you find that you are getting file related errors, here’s one (of many) solutions—open a terminal window and follow these steps below making sure to replace the users, paths, and folders given in this example with your own.

  1. Create a new user group, I’ll call it www‑moi. sudo groupadd www-moi
  2. Add both the user you log in as, example, moi, and the user running the Apache server process, example, www-data, to the new group, www-moi.

    On a Linux/Mac device, open a terminal emulator, type:

    sudo usermod -a -G www-moi www-data

    Press Enter.

  3. Change the group ownership of the regex_tester folder to www-moi.

    On a Linux/Mac device, open a terminal emulator, type:

    sudo chgrp -R www-moi /your/path/to/htdocs/regex_tester

    Press Enter.

  4. Give the www-moi owner and group read/write/execute permissions on the regex_tester folder.

    On a Linux/Mac device, open a terminal emulator, type:

    sudo chmod -R 770 /your/path/to/htdocs/regex_tester

    Press Enter.

  5. Log out, log back in, then check your handiwork.

    On a Linux/Mac device, open a terminal emulator, type:

    ls -l /your/path/to/htdocs/regex_tester

    Press Enter.

Screenshot of regex tester

A kangaroo is neither cat nor dog. Thanks for clearing this up, regular expressions!

Resources:

«  »

⛵ top ⛵

One lonely comment — it deserves company. . .

  1. Jen comments:

    Honk!

Expound upon: A preg_match() Regular Expression Tester for Your Development Server

Your email address will not be published. Required fields are marked *

*