Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regex syntax is supported by the =~ operator in bash?

Tags:

bash

I discovered that I can use =~ operator instead of expr command in my 4.2.10(1) BASH. It is much faster (within a command) than expr and this fact could be important inside in a loop with large repetition.

I was able to use most of the meta characters of regular expression but not all.

For example I can check a string matches exactly 3 repetitions of (one small letter, one digit, one dot):

[[ "b3.f5.h3." =~ ^([a-z][0-9]\.){3}$  ]] && echo OK
OK

and I can select matched substrings:

[[ "whatis12345thetwo765nmbers" =~ ^[a-z]+([0-9]+)[a-z]+([0-9]+) ]] && \
echo "The two number fields are: ${BASH_REMATCH[1]}  ${BASH_REMATCH[2]}"
The two number fields are: 12345  765

But I would like to use more meta characters, such as the ones listed on this TLDP page.

I would especially like to match word boundaries: \b, \B, \<, \> .

I tried to find an answer in the Advanced Bash-Scripting Guide (in Chapters 18 and 37) but was unsuccessful.

Where can I find a detailed description of =~ operator?

At the moment I am interested only in BASH and not in gawk, sed, perl or other tools.

like image 824
László Szilágyi Avatar asked Sep 15 '25 08:09

László Szilágyi


1 Answers

=~ supports POSIX ERE with no extensions additional to those added by the local C library (literally, it calls the standard C library's regex calls). Thus, the canonical documentation on features it's guaranteed to support (as opposed to optional features your local C library may add in addition) is the specification on ERE, IEEE 1003.1, section 9.4.


To amplify this: Anything, such as \<, added by one particular libc (ie. glibc) but not present in the POSIX specification cannot be expected to work portably across all platforms bash supports.

The POSIX-specified special characters (as given in section 9.4.3 of the standard) do not include <, >, b or B; these are all GNU extensions and nonportable.

like image 124
Charles Duffy Avatar answered Sep 17 '25 03:09

Charles Duffy