I often find my self, searching for a way to test some RegExs and almost every time, i use an online tool. I searched for a CLI alternative, but found them inadequate for me. Either they need to much dependencies, like external Python libraries or they don't have a good documentation, providing examples and use cases for the various regex symbols/formats. So... i made my own, with tools all ready had installed on my system and a BASH script.
What you will need...
The tools you will need are these:
tmuxuphttps://github.com/akavel/upbatorless- the script below...
How it works...
The script launches tmux. It automatically creates two panes. In the bottom pane, their some text, with regex examples and instructions on how to use regexs. It uses less or bat to navigate through the document.
At the top pane, it launches the program up (Ultimate Plumber), which is an awesome tool, to test PIPE commands in Linux. I use it all the time and if you try it, you will also too. So, using up, the script displays a file the user wants to test his RegExs, with cat and passes it to grep which handles the RegEx commands. This way, you write your RegEx string, press Enter, and watch immediately, the results. Instantly, you can alter the program (grep in this case) and use whatever you want, like Perl or Awk, for testing the RegEx strings.
If you find, that you use the same RegEx strings, over and over, you can simple edit the script, place them at the document/help text area and store them for future use. It's a tool, that you can customize in which ever way you want. By using the grep command, you can just add parameters, that will give you even more useful results. For example, while testing a regex string, you can just add the -c parameter, to just count, how many matches you have... that simple!
Just copy/paste it, make it executable and use like: regex <filename> where filename, is the file you want to test the regex strings.
The script is built in such a way, that it will work, either if you run it from inside tmuxor even iftmux` is not running at the time.
#!/bin/bash
thisfile=`basename "$0"`
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [[ -n "$TMUX" ]]; then
tmux new-window -n "regex" -d
else
tmux new-session -d -s "regex" -n "main"
fi
tmux split-window -v -p 30
tmux select-pane -t 1
tmux send-keys -t 1 "bash" C-m
sleep 0.5 # Give bash time to start
tmux send-keys -t 1 "cat $1 | up -c 'grep -P ' " C-m
tmux send-keys -t 2 "cat $SCRIPT_DIR/$thisfile | tail -n+24 | sed 's/#//g' | bat" C-m
if [[ -n "$TMUX" ]]; then
tmux select-window -t "regex"
else
tmux attach-session -t "regex"
fi
#*egrep parameters*
#-i Ignore case (ie uppercase, lowercase letters).
#-v Return all lines which don't match the pattern.
#-w Select only matches that form whole words.
#-c Print a count of matching lines.
#Can be combined with the -v option to print a count of non matchine
#lines.
#-l Print the name of each file which contains a match.
#Normally used when grep is invoked with wildcards for the file
#argument.
#-n Print the line number before each line that matches.
#---
#*Regular Expressions*
#. A single character
#[abc] Range. ie any one of these characters
#[^abc] Not range. A character that is not one of those enclosed.
#(abc) Group these characters and remember for later.
#\n Replace n with a number. Recall the charactes matched in that
#set of brackets.
#| The logical 'or' operation.
#\ In front of a character, removes it's special meaning.
#RE Multipliers
#? The preceding item is optional, it is matched zero or one times.
#* The preceding item will be matched zero or more times.
#+ The preceding item will be matched one or more times.
#{n} The preceding item will be matched exactly n times.
#{n,} The preceding item will be matched n or more times.
#{n,m} The preceding item will be matched between n and m times.
#RE Anchors
#^ From the beginning of the line.
#$ To the end of the line.
#\< At the beginning of a word.
#\> At the end of a word.
#\a Matches a bell character
#\t Matches a tab
#\r Matches a carriage return
#\v Matches a vertical tab
#\f Matches a form feed
#\n Matches a new line
#\e Matches an escape
#\A The match must occur at the start of the string. \A\d{3}
#"913" in "913-333-"
#\Z The match must occur at the end of the string or before a new
#line at the end of the string. -\d{3}\Z "-333" in "-913-333"
#\z The match must occur at the end of the string. -\d{3}\z
#"-333" in "-913-333"
#\G The match must occur at the point where the previous match
#ended. \G\(\d\) "(1)", "(3)", "(5)" in "(1)(3)(5)[7](9)"
#\b The match must occur on a boundary between a \w (alphanumeric)
#and a \W (non-alphanumeric) character. \bscheme\b "scheme" in
#"That was a clever scheme"
#\B The match must not occur on a \b boundary. \Band\w*\b "ands",
#"ander" in "and sands android
#a|b Matches either a or b
#(?(exp)yes|no) yes if exp is matched; no if exp isn't matched
#(?(name)yes|no) yes if name is matched; no if name isn't matched
#1. Digits
#Whole Numbers: ^\d+$
#Decimal Numbers: ^\d*\.\d+$
#Whole + Decimal Numbers: ^[\-+]?\d*(\.\d+)?$
#Negative, Positive Whole + Decimal Numbers: ^-?\d*(\.\d+)?$
#Whole + Decimal + Fractions: [-]?[0-9]+[,.]?[0-9]*([\/][0-9]+[,.]?[0-9]*)*
#2. Alphanumeric Characters
#Alphanumeric without space: ^[a-zA-Z0-9]+$
#Alphanumeric with space: ^[a-zA-Z0-9 ]*$
#3. Email
#Common Email Address:
#^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$
#4. Dates
#Format YYYY-MM-dd:
#^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$
#Format MM-dd-YYYY:
#^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$
#Format dd-MM-YYYY:
#^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$
#5. Time
#Format HH:MM 12-hour, optional leading 0:
#^(0?[1-9]|1[0-2]):[0-5][0-9]$
#Format HH:MM 12-hour, optional leading 0, AM/PM:
#^((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))$
#HH:MM 24-hour with leading 0:
#^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
#HH:MM 24-hour, optional leading 0:
#^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
#HH:MM:SS 24-hour:
#^(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)$
#6. Match Duplicates
#Search Duplicates: (\b\w+\b)(?=.*\b\1\b)
#7. Phone Numbers
#US and International Phone Numbers with Separators:
#^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
#US Phone Numbers Only with Separators:
#^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
#Unformatted Phone Numbers:
#^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
#8. Identification
#Social Security Number with dashes:
#^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$
#Social Security Number without dashes:
#^(?!219099999|078051120)(?!666|000|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}$
#9. Zip Codes
#US Postal Code: ^\d{5}([\-]?\d{4})?$


