Trying to come up with a regexp

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
I have a list of folders and files and I need a regexp that can match the following path and anything under it:

/path/to/folder1

So it should match:
/path/to/folder1
/path/to/folder1/sub1
/path/to/folder1/sub2/sub3

but it should NOT match:
/other/path/to/folder1
/path/to/folder2

Ok so far I have this:
^/path/to/folder1[something]*

but I don't know what to put in the brackets to match everything. I can do something like [0-9a-zA-Z] but that doesn't take into account backslashes and the possibilities of underscores, dashes, tildes... the list could go on and on. I could include all those in the regexp, but that would get tedious and I think there would be a better way.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
Oh, I think I found it. It's the ".". So my expression would be:
^/path/to/folder1.*

If I'm understanding it right, I don't need the brackets here.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
Now my only issues is how is it going to handle variable substituion? The "folder1" will actually come from a variable, so it will be:
^/path/to/${var1}.*

so is the ${var1} going to be substituded as a variable or is it going to use the $ as part of a regexp or something funky?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Regular expressions differ slightly depending on what language you're using them in, but you'll probably have to escape at least the $ so it's not taken as an end of line match.
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
This is in a BASH script. I guess I should have specified that. I did test it and it works PERFECT.

This is actually a continuation of this thread: http://forums.anandtech.com/me...085599&highlight_key=y
from 2 months ago (which you also weighed in on). I just never got around to figuring out the regexp. Here is the whole script:

SNAP=Snap1
for link in $( find /export/share/ -maxdepth 4 -type l ); do
if [[ `readlink $link` =~ "^/export/snaps/${SNAP}.*" ]]; then
rm $link
fi
done

It finds every symlink under /export/share and if it points somewhere under /export/snaps/Snap1 then it gets removed.