From the course: Bash Patterns and Regular Expressions

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using BASH_REMATCH

Using BASH_REMATCH - Bash Tutorial

From the course: Bash Patterns and Regular Expressions

Start my 1-month free trial

Using BASH_REMATCH

- [Instructor] We can use backreferences in regular expressions which gives us a lot of power to replay partial matches inside of a pattern. I won't go over backreferences again as we've already covered them but Bashes in process regular expressions provide an additional functionality that's similar. It's called BASH_REMATCH. Think of REMatch as Regular Expression Match, not the English word Rematch. If a Bash regular expression matches a string, the return code is zero and the matched portion is placed inside the BASH_REMATCH array. For instance, if we have a string of abcdef, and a Regex of b.d which is b followed by any character of any type, and ending was d, then the return code would be zero and the contents of the first BASH_REMATCH array would be bcd which in this case is the entire pattern match. If there is no match, then the return code from the if conditional is one and BASH_REMATCH is null. This is a lot like backreferences but in some ways more flexible and powerful as…

Contents