Regex for matching directory path depth

Probably this:

#!/usr/bin/env bash

array=()
string="/packages/frontend/react-app/src/index.ts"

[[ $string =~ packages/([^/]+/){2} ]] && array+=("${BASH_REMATCH[0]}")

Explanation:

[^/]+ match any non-empty string that does not contain a /.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top