LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2011, 07:53 PM   #1
davejjj
LQ Newbie
 
Registered: Oct 2011
Posts: 19

Rep: Reputation: Disabled
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]/'
 
Old 11-07-2011, 08:25 PM   #2
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by davejjj View Post
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 View Post
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 View Post
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

Last edited by SigTerm; 11-07-2011 at 08:27 PM.
 
Old 11-07-2011, 08:36 PM   #3
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
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".
 
Old 11-07-2011, 08:46 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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

Last edited by Telengard; 11-07-2011 at 08:47 PM. Reason: fix code tag
 
1 members found this post helpful.
Old 11-08-2011, 10:30 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 11-08-2011, 04:20 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
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
 
1 members found this post helpful.
  


Reply

Tags
regex, regular expression, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions Wim Sturkenboom Programming 10 11-19-2009 02:21 AM
LXer: RegEx Buddy--A Simple Way to Create Regular Expressions LXer Syndicated Linux News 0 02-15-2009 03:01 AM
help with regular expressions mariogarcia Linux - Software 3 01-28-2009 04:23 AM
regular expressions. stomach Linux - Software 1 02-10-2006 07:41 AM
regular expressions? alaios Linux - General 2 06-11-2003 04:51 PM


All times are GMT -5. The time now is 12:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration