• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Unix: Regular Expression - what's better?

imported_Stew

Golden Member
So, say I want to list 3-letter files whose names are composed of a b and c.

I can do:
ls [abc][abc][abc]

That works.

But what's a better way to do that, using wildcards or anything else?
 
well, you could do (i'm a little regular expression rusty)
ls ^[a-c]\{3}

That would be any line starting with 3 a, b, or c's. But that is not quite what you want. Maybe something like (not sure if this is valid)
^[a-c]\{2\}[a-c]$

That's 2 a, b, or c's at the begining, and 1 a, b, or c at the end.
 
This should work:
ls [a|b|c]{3}

If you want to include capital letters, you'll have to do this:
ls [a|b|c|A|B|C]{3}

hmmm, testing it out, it didn't work. Bash seems to want to pipe as if it's piping to a new command rather than as part of the regular expression.

and actually I think I should have used () instead of []
 
There's nothing wrong with the originally working solution:
ls [abc][abc][abc]

bash doesn't support regex(3) regexps in globbing its input.
Stuff works in sed (which does use full regex(3) regexps) that doesn't work no matter how it is quoted on the command line:

echo aaa bab caa | sed -e "s,[a-c]\{3\},foo,g"
foo foo foo

 
Back
Top