LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-21-2012, 08:06 PM   #1
austinchen
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
what's the best way to understand regular expression


Hi guys, i am new to linux, and recently i have serious trouble mastering regular expression. there are so many rules and special character, could you guys tell me what's the best way to understand regular expression. Thanks
 
Old 11-22-2012, 12:03 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 598

Rep: Reputation: 120Reputation: 120
I used this site when I started with RE's and ERE's : http://www.grymoire.com/Unix/Regular.html

Here's another : http://etext.lib.virginia.edu/servic...nix/regex.html

The best way to learn it is to put it to use. I had to learn it before I could understand sed. Start with the basics. grep uses RE's and ERE's and is pretty much simple to use. Use it to find simple strings of text in files.

Create a simple multiline text file and use grep to find various combinations of strings at the beginning, within, or at the end of a line.

After getting a fair understanding of RE's, I would browse the threads looking for posts specific to sed and RE's and try to come up with a solution.

Again, practice makes perfect.
 
1 members found this post helpful.
Old 11-22-2012, 01:02 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,412

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You should know that different tools and languages often have different engines/rules, even though they may overlap somewhat. If you really want to know, I highly recommend this book http://regex.info/index.html, here's the Contents list http://regex.info/toc3.html and any material you can find on the tool/lang of your choice.
 
1 members found this post helpful.
Old 11-22-2012, 01:18 AM   #4
austinchen
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by towheedm View Post
I used this site when I started with RE's and ERE's : http://www.grymoire.com/Unix/Regular.html

Here's another : http://etext.lib.virginia.edu/servic...nix/regex.html

The best way to learn it is to put it to use. I had to learn it before I could understand sed. Start with the basics. grep uses RE's and ERE's and is pretty much simple to use. Use it to find simple strings of text in files.

Create a simple multiline text file and use grep to find various combinations of strings at the beginning, within, or at the end of a line.

After getting a fair understanding of RE's, I would browse the threads looking for posts specific to sed and RE's and try to come up with a solution.

Again, practice makes perfect.
Thank you for your helping advices
 
Old 11-22-2012, 02:02 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
chrism1 is right, the book of Jeffrey Friedl is very valuable.

Another point is that you recognize that different tools have a (slightly) different set of regular expressions, but this can be handled with a little experience.
Perl is the state of the art regarding regular expressions. If you use sed, you will notice that it's regular expressions differ from those in Perl primarily in which characters have to be escaped, for example the '.' for "any character" has to be escaped with a backslash in sed. The vi/vim editors have the same roots as sed and therefore use the same set of regular expressions.

Markus
 
Old 11-22-2012, 02:38 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,412

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Speaking of Perl, some tools have a -pcre (Perl Compatible Regular Expression) option, so that you can use the Perl regex engine rather than the one native to that tool.
Quote:
Most modern programming languages offer primitive pattern-matching tools, usually through an extra library. In contrast, Perl's patterns are integrated directly into the language core. Perl's pattern matching boasts features not found elsewhere, features that encourage a whole different way of looking at data. Just as chess players see patterns in the board positions that their pieces control, Perl adepts look at data in terms of patterns. These patterns, expressed in the intensely symbolic notation of regular expressions,[8] provide access to powerful algorithms normally available only to scholars of computer science.

[8]Technically, Perl's patterns far exceed the capabilities of mere regular expressions as that term is formally used in computing theory.
Perl Cookbook 2nd Ed, Chap 6. Pattern Matching

 
Old 11-22-2012, 02:45 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,797

Rep: Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888Reputation: 2888
a bit against those books: you need practice. I think you need to find some tasks to solve and "do it yourself". Of course you will need to read tutorials. Also, as it was already mentioned it can be language specific, so for example if you want to learn regexp in perl you will need this page: http://perldoc.perl.org/perlretut.html
 
Old 11-22-2012, 06:42 AM   #8
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
a bit against those books: you need practice. I think you need to find some tasks to solve and "do it yourself". Of course you will need to read tutorials. Also, as it was already mentioned it can be language specific, so for example if you want to learn regexp in perl you will need this page: http://perldoc.perl.org/perlretut.html
This is true, but as for the book of Jeffrey Friedl, I found it very exciting to read.

And as of practice: before reading Friedl I found regex difficult, but once I had read the first pages, I began to use regular expression as often as possible. For example when editing with vim, formerly I used to do many tasks with macros, now I use regex instead.

Markus
 
  


Reply


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 expression for example.* prayag_pjs Linux - Newbie 3 02-17-2012 05:40 PM
Regular Expression 0.o Programming 3 06-09-2009 03:28 AM
Regular Expression harkonen Programming 6 07-12-2008 01:06 PM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 08:48 PM
regular expression gumby Programming 3 07-15-2003 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration