From b8b8a5cfabd72dcc2fae0900ed739c5574bc3a1d Mon Sep 17 00:00:00 2001 From: Lars Windolf Date: Wed, 15 Nov 2023 00:39:21 +0100 Subject: [PATCH] Update Bash Regex.md --- cheat-sheet/Scripting/Bash Regex.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cheat-sheet/Scripting/Bash Regex.md b/cheat-sheet/Scripting/Bash Regex.md index c55613306..103d822c7 100644 --- a/cheat-sheet/Scripting/Bash Regex.md +++ b/cheat-sheet/Scripting/Bash Regex.md @@ -3,24 +3,24 @@ related: cheat-sheet: ['Bash'] --- -### Regexp Matching +### Regex Matching Use conditions with doubled [] and the =\~ operator. Ensure not to quote -the regular expression. Only BRE are allowed. If the regexp has +the regular expression. Only BRE are allowed. If the regex has whitespaces put it in a variable first. if [[ $string =~ ^[0-9]+$ ]]; then echo "Is a number" fi -### Regexp Match Extraction +### Regex Match Extraction **Variant \#1:** You can do this with grouping in bash. Despite only BRE -being supported grouping works also. Note how you need to set the regexp +being supported grouping works also. Note how you need to set the regex into a variable because you must not quote it in the if condition! - REGEXP="2013:06:23 ([0-9]+):([0-9]+)" - if [[ $string =~ $REGEXP ]]; then + REGEX="2013:06:23 ([0-9]+):([0-9]+)" + if [[ $string =~ $REGEX ]]; then echo "Hour ${BASH_REMATCH[1]} Minute ${BASH_REMATCH[2]}" fi