Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Question about regular expressions

A reader of Learning Unix for Mac OS X Panther writes in to ask for an explanation of the regular expression shown in this command:
du -s * .[^.]*

Dave's Answer:
This regular expression is easily understood if you remember that to exclude a character from an expression you need to negate it with the ^, but to do that, you need to set off what you're negating from the rest of the expression - the [ ] part.

So now you should be able to see that the expression matches all files or directories that begin with a dot but don't have a dot as their second character, followed by zero or more characters.

In essence, this regular expression matches any dot files other than '..'.

If this seems overwhelming and you're still quite interested in learning more about regular expressions, there's a very good book from O'Reilly about regular expressions called Mastering Regular Expressions. It's not poolside reading, but it'll get you to the next level.


Related Unix and Linux Help articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 5

sreeju said, on June 12, 2005 7:27 AM:

can you please email me the regular expression that can limit the occurances of the same number for eg.

if you are limiting the maximum occurance to 3 then no number can repeat more than thrice in the string

eg. 1122338765 should be valid

but 1122224564 should not not be valid

egkamp said, on June 21, 2005 8:52 PM:

but should't
du .[^.]*
mean
'.'
followed by
anything not a '.'
followed by 0 or more of
anything not a '.'
yet it hits for .a.b.c
why does [^.]* mean "not a dot" followed by "0 or more of any char", rather than
"not a dot" followed by "0 or more or the preceding (which was "not a dot")"

monsieur said, on May 17, 2006 5:33 AM:

bash does NOT use regular expressions. Bash uses FILE GLOBS (see section "Pattern Matching" in bash).

DAZ said, on February 17, 2007 5:37 AM:

IT SAYS THAT INTERNET BROWSER DO NOT START ON SETTINGS SO HOW DO I CHANGE IT

Scott said, on May 2, 2012 8:05 AM:

My dataset has a column with a numeric code (25xx). I use RegEx to remove specific data that fall outside a numeric code range 25xx - 25xx. As long as the number values are in a 4 digit format everything works fine, but some datasets have all of the numeric codes with a hyphen (-) Ex. 25-21, 25-66. I need to remove the hyphen so the RegEx works correctly, but can't seem to get the right RegEx to do this.

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.