Useful RegEx Snippets
RegEx Snippets
These are some useful RegEx snippets that I find myself using over and over. This is a list that will be updated often
Selecting all the trailing whitespace (tabs or space)
1
[ \t]+$
Select everything between two words
1
(.*)
Remove all blank lines
1
^(?:[\t ]*(?:\r?\n|\r))+
Example: Lets remove the blank lines from this config
1
2
3
4
5
6
config firewall address
edit "*.evercast-webrtc.com"
set type fqdn
set fqdn "*.evercast-webrtc.com"
next
Open Search and Replace and type in the RegEx above and the results will be
1
2
3
4
5
config firewall address
edit "*.evercast-webrtc.com"
set type fqdn
set fqdn "*.evercast-webrtc.com"
next
Select everything up until specified character
1
^[^$]*
Select everything, from a character/word/other, to the end. Including blank spaces
1
([ ,]+)(connected)(.*)
Example: Lets remove everything except the interface name
1
2
# Cisco switchport
Gi1/0/30 connected 107 a-full a-1000 10/100/1000BaseTX
Open Search and Replace and type in the RegEx above and the results will be
1
Gi1/0/30
Select all characters up to a specific character
If you need to select everything up until a specific character, you can use the following
Example: 160117.070: CMD: ‘show stackwise-virtual links’ 16:44:44 PDT Mon Sep 11 2023
1
160([^:]*):
Will select 160117.070: up until the first “:”
IPv4 Addresses
Found myself needing to parse some logs for IPv4 addresses recently and a colleague helped me out and send this over my way.
Example: You want to filter out IPv4 addresses that start with 192.168.x.x
1
2
3
# all 4 octets
# (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
192.168.(\d{1,3}\.\d{1,3})
-eof-