๐Ÿ“‚ regex

โšก regex::escapeRegexSpecialChars

Escapes special characters in a string to be used as a regex.

Inputs:

  • $1: string to escape as string:

    The string to escape.

Returns:

  • ${REPLY}: The escaped string.

Example usage:

regex::escapeRegexSpecialChars "a.(b)"
echo "${REPLY}"

โšก regex::getFirstGroup

Matches a string against a regex and returns the first captured group of the first match.

Inputs:

  • $1: string variable name as string:

    The variable name containing the string to match.

  • $2: regex as string:

    The regex to use for the match.

Returns:

  • ${REPLY}: The first capture group in the matched string. Empty if no match.

Example usage:

MY_STRING="name: julien"
regex::getFirstGroup MY_STRING "name:(.*)"
echo "${REPLY}"

Regex wiki: https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions

โšก regex::getFuzzySearchRegexFromSearchString

Allows to get a regex that can be used to fuzzy search a string. the -> ‘([^t]*)(t[^h]*h[^e]*e)’

Inputs:

  • $1: search string as string:

    The variable name containing the search string to match.

Returns:

  • ${_STRING_FUZZY_FILTER_REGEX}: the regex

Example usage:

regex::getFuzzySearchRegexFromSearchString SEARCH_STRING
echo "${_STRING_FUZZY_FILTER_REGEX}"

โšก regex::getMatches

Returns an array containing all the matched for a regex in a string.

Inputs:

  • $1: string variable name as string:

    The variable name containing the string to match.

  • $2: regex as string:

    The regex to use for the match.

  • ${replacement} as string:

    (optional) The replacement string to use on each match.

    • Use \x to refer to the x-th capture group.
    • Use \c to refer to replacement counter.

    Set to an empty string to keep the matches as they are.

    (defaults to “”)

  • ${max} as int:

    (optional) The number of matches to return. Set to -1 for unlimited replacements.

    (defaults to -1)

Returns:

  • ${REPLY_ARRAY[@]}: An array containing all the matches.

Example usage:

MY_STRING="name: julien, name: john"
regex::getMatches MY_STRING "name: (.*)"
regex::getMatches MY_STRING "name: (.*)" max=1
for match in "${REPLY_ARRAY[@]}"; do
  echo "${match}"
done

Regex wiki: https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions

โšก regex::replace

Replaces strings within a string using a regex (replaces in place).

Inputs:

  • $1: string variable name as string:

    The variable name containing the string in which to do replacements.

  • $2: regex as string:

    The regex to use for the match.

  • $3: replacement string as string:

    The replacement string. Use \x to refer to the x-th capture group. Use \c to refer to replacement counter.

  • ${max} as int:

    (optional) The number of replacements to do. Set to -1 for unlimited replacements.

    (defaults to -1)

  • ${onlyMatches} as bool:

    (optional) Instead of replacing with the regex, we keep only the matches. This can be used to extract information from a string.

    (defaults to false)

Example usage:

MY_STRING="name: julien"
regex::replace MY_STRING "name: (.*)" "\1"
regex::replace MY_STRING "name: (.*)" "\1" maxCount=1 onlyMatches=true
echo "${MY_STRING}"

Regex wiki: https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions

Important

Documentation generated for the version 0.33.0 (2025-08-31).