summaryrefslogtreecommitdiffstats
path: root/modules/regex.inc
blob: 3188b2dfb196cb6d5f56a01a8d00d54a25ef2c24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/ash

# Match $1 against perl regex $2, case sensitive
regex_match() {
	printf "%s" "$1" | grep -qP "$2"
}

# Match $1 against perl regex $2, case insensitive
regex_imatch() {
	printf "%s" "$1" | grep -qPi "$2"
}

# Escapes $1 for use in grep -E/-P, sed -r, etc.
# Pass --slash to escape '/' too. (e.g. for sed)
regex_escape() {
	if [ "x$1" = "x--slash" ]; then
		printf "%s" "$2" | sed 's,[][()\.^$?*+/],\\&,g'
	else
		printf "%s" "$1" | sed 's/[][()\.^$?*+]/\\&/g'
	fi
}