LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Learning RegEx? Regular Expressions (https://www.linuxquestions.org/questions/programming-9/learning-regex-regular-expressions-912390/)

davejjj 11-07-2011 06:53 PM

Learning RegEx? Regular Expressions
 
I have looked at a number of online tutorials but I am becoming convinced that I will need to buy a book on regex to ever understand it. Isn't this really the only way to learn it? Opinions? Thanks.

Example problem: use Linux sed or tr to capitalize the first word of a string. Gosh that sounds easy! Probably just sed 's/^./[A-Z]/' or maybe sed 's/^[a-z]/[A-Z]/'

SigTerm 11-07-2011 07:25 PM

Quote:

Originally Posted by davejjj (Post 4518406)
I have looked at a number of online tutorials but I am becoming convinced that I will need to buy a book on regex to ever understand it.

Here are tutorials for regexps and sed.

Quote:

Originally Posted by davejjj (Post 4518406)
Isn't this really the only way to learn it?

No. There is plenty of information and tutorials, so there's no reason to waste your money on a book. You could read "man perlre" and still learn a lot.

Quote:

Originally Posted by davejjj (Post 4518406)
Example problem: use Linux sed or tr to capitalize the first word of a string. Gosh that sounds easy!

You could use google search, you know. Try sed capitalize

Juako 11-07-2011 07:36 PM

Of course a book isn't the only way to learn regex. What really makes you learn is practice and a good tutorial, apart from frequent use of "info sed" for help on sed regexes, and "man 7 regex" for info on POSIX regexes.

BTW, in the info page for sed I find a page linked: http://sed.sourceforge.net/grabbag

And google for "sed one liners", it's a very well known doc with the more common sed tasks. There's also the annotated version: "sed one liners explained".

Telengard 11-07-2011 07:46 PM

Start with the regex manpage. It will be your starting point and reference as you study regular expressions.

Code:

man 7 regex
Understand that there are two general forms for regular expressions. The older of the two is largely considered deprecated, though it still sees use and continues to be supported for backward compatibility.

Further understand that every program which accepts regular expressions has its own rules. You must consult the program's documentation to understand its regular expression syntax.

For tutorials, I recommend reading at least two of the following:

http://www.scootersoftware.com/RegEx.html
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/
http://www.ibm.com/developerworks/ai...ps3/index.html
http://www.ibm.com/developerworks/li...3-7/index.html

HTH

David the H. 11-08-2011 09:30 AM

Note that it's not just about learning regex, it's also about learning sed and the other tools. I learned quite a bit from the sedfaq, including how to capitalize a string.

http://sed.sourceforge.net/sedfaq.html

Code:

$ echo "david" | sed 's/.*/\u&/'
David

But yeah, in the end it just comes down to practice and experience. It takes time to learn anything complex, and regex is certainly complex. Just keep at it.

Tinkster 11-08-2011 03:20 PM

And while "all the info" is out there and comprehensive, it's not always
easy to understand. I find no harm in buying a book, and happily recommend
Jeffrey Friedel's "Mastering regular expressions", which is not only very
accurate and exhaustive on the subject, but also actually fun to read.



Cheers,
Tink


All times are GMT -5. The time now is 06:46 PM.