If I understand you correctly, you want a regex that will allow you to use
grep to identify paths. I am not sure what you are trying to do with the regex you have posted. Why do you want something to be repeated? Paths only need a single slash:
Anyway, if you want any kind of path (paths can also contain non-word characters like
( spaces and new lines etc), try this:
grep -E '/[^/]+' *txt
| |--------> one or more
|-----------> A character class, '^' in a character class means NOT,
so this class means "anything that is not /".
grep that the pattern you will give it shoujld be interpreted as an Extended Regular Expression rather than it's default, Basic Regular Expression. EREs support
+ for "one or more" which is needed to find only strings with at least one non-slash character after the first slash.
If you want to find only paths with more than a single
/, you can do something like
grep -E '/[^/]+/[^/]+' *txt
Paths can also end with a slash, if for some reason you want to keep such trailing slashes use (
/? means "find zero or one
grep -E '/[^/]+/[^/]+/?' *txt
More specifically, the regex you are using fails for various reasons. First of all, as @erewok pointed out, you are using parentheses and a
+ inside brackets. Since brackets specify a character class, anything within them is treated as one of the characters to be found (with the exception of
^ which makes it a negated character class).
[(/\w+)]+ means find any of
/, any word character (
) one or more times. In any case,
\w is not recognized by
grep unless you use Perl Compatible Regular Expressions. You can activate these in
grep with the
-P flag. For example, this will match a path like
grep -P '/\w+' *txt
If you know that your paths will always consist of word characters (ie a-z,A-Z,0-9, and
_), you can use an expression like the above but since one can never be sure, using something less strict, lke my fist suggestion is preferable.