Tags » Regex

regex: extract LDAP cn value from dn string

Regex snippet to extract the cn value from a dn string for LDAP:

Input:

cn=smit1057,ou=ACTIVE,ou=PEOPLE,o=DATA

Target Output:

smit1057

Regex:

/^cn=([^,]+)/$1/
Snippets

regex: attribute list to Ruby hash

Snippet to convert a list of attributes to a Ruby hash.

Input:

cn:ID
sn:Surname
fn:FirstName

Target output:

"cn" => "ID",
"sn" => "Surname",
"fn" => "FirstName",

Regex:

/(.+?)(?=\:):(.*$)/"$1" => "$2",/
Snippets

split string in multiple lines

split csv string in multiple lines

echo 'a,b,c' | sed 's/,/\n/g'

split | (pipe) regex string in multiple lines

echo 'a|b|c' | sed 's/|/\n/g'
Regex

analyzes apache log and blocking url access 3 times in X minutes

block multiple URLs

#!/bin/bash
# analyzes apache log and blocking ip than access url 3 times in 2 minutes
# block only access to url not to all urls

# you can use | to separate multiple strings to block
URL_STRING_TO_BLOCK="insertPartOfUrl1Here|insertPartOfUrl2Here"

RANGE_TIME=$( date '+%H:%M' )

# make array with 2 past minutes using | separator. 311 more words
Shell Script

regex to math with range numbers

regex to match 10 to 15 in date time like: 2017/11/16 17:

grep '2017\/11\/16.*17:(10|11|12|13|14|15)'
Regex

filter specific day in apache log

grep of current day in apache

grep "$(date +%d/%b/%Y)" /var/log/httpd/access.log

you can insert part of url to filter too

grep "$(date +%d/%b/%Y).*urlToFilterHere" /var/log/httpd/access.log

grep of… 295 more words

Regex

REGEX patterns

A website, HTMLPattern.com, that has the regex for almost every kind of validation you’d ever want to create.