LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-02-2016, 01:42 AM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,783

Rep: Reputation: 107Reputation: 107
Unhappy How does everybody survive with regex?


Hya,

Question
How does everybody survive with regex?

Situation
There are several versions(?) of regex in our Penguin world.
basic regex (obsolete regex, per man regex)
extended regex (modern?)
.
findutils-default, awk, egrep, ed, emacs, gnu-awk, grep, posix-awk, posix-basic, posix-egrep, posix-extended, posix-minimal-basic, sed

just to name a few.

and similar but different idea, (shell filename substitution)

Closing remark
Happy Penguins ?
 
Old 02-02-2016, 01:48 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,970

Rep: Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633
No mention of perl-re ?.

 
Old 02-02-2016, 01:55 AM   #3
ferrari
LQ Guru
 
Registered: Sep 2003
Location: Auckland, NZ
Distribution: openSUSE Leap
Posts: 5,354

Rep: Reputation: 1038Reputation: 1038Reputation: 1038Reputation: 1038Reputation: 1038Reputation: 1038Reputation: 1038Reputation: 1038
You're going to get 100 different responses.... it's a very deep pool

Some excellent real-world regular expression examples provided here:

http://blog.codinghorror.com/regular...-two-problems/
 
Old 02-02-2016, 07:44 AM   #4
polaris96
Member
 
Registered: Jan 2015
Distribution: Slackware, LFS, OpenIndiana, debian wheezy
Posts: 55

Rep: Reputation: Disabled
Think of regex variants like shell variants. For basic stuff, I've never really needed to be aware of the differences. If my search keys are so esoteric that the flavour of regex will effect the output, I'm probably going to break up the search and iterate through it (awk, usually)

Complicated regex aloritha have always made my head hurt. They're notoriously difficult to test. There always winds up being some wacky bit of data that you forgot to test against "false flagging" your script.

I just type a few more lines and do my search in stages. Simple to troubleshoot. Works in all flavours of regex, and it;'s friendly to the next poor soul that needs to understand what I intended.
 
Old 02-02-2016, 10:47 PM   #5
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,783

Original Poster
Rep: Reputation: 107Reputation: 107
Hya

I guess I sank deep in this pool.

Even basic (my opinion) things like
Code:
grep '\(.\).*\1\(.\).*\2'
grep -E '(.).*\1(.).*\2'
are sometimes confusing for me.
 
Old 02-03-2016, 10:57 AM   #6
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 5,842

Rep: Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162Reputation: 2162
Quote:
Originally Posted by kaz2100 View Post
How does everybody survive with regex?
Simple! I never use it.
 
1 members found this post helpful.
Old 02-03-2016, 06:00 PM   #7
twork
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Rep: Reputation: Disabled
I don't usually like to plug products or books, but my saving grace was the O'Reilly regex book:

http://shop.oreilly.com/product/9780596528126.do

The Wikipedia page on regex has a decent quick-ref chart:

https://en.wikipedia.org/wiki/Regula...racter_classes

...but if you're struggling, and you expect to do anything involving any kind of code for any length of time, I highly, highly recommend buying or borrowing the O'Reilly book. Just the first chapter, "Introduction to Regular Expressions," made me want to never look back. Later chapters get into deeper techniques and specifics (like the yes-maddening differences between different languages and tools), but just that one chapter changed the whole way I interact with text.
 
Old 02-03-2016, 06:05 PM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,964

Rep: Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798
Quote:
Originally Posted by DavidMcCann View Post
Simple! I never use it.
Denial is not just a river in Egypt
 
Old 02-03-2016, 07:07 PM   #9
twork
LQ Newbie
 
Registered: Mar 2015
Posts: 20

Rep: Reputation: Disabled
Oh and:

Quote:
Originally Posted by kaz2100 View Post

Even basic (my opinion) things like
Code:
grep '\(.\).*\1\(.\).*\2'
grep -E '(.).*\1(.).*\2'
are sometimes confusing for me.
Not sure, but it may be that some of your trouble comes from the shell, not from regex per se.

The shell sees symbols like "()", ".",
Code:
\
and so forth as significant, and depending on what you're doing, it may go something like,

Code:
[human types something] 
  => [shell eats the symbols it recognizes and turns the poor human's text into something unintended] 
  => [tool the human really meant to use gets fed something the human did not intend; behaves accordingly, spits out crap]
  => [regex gets the blame]
How to get around that varies with the situation, and it can be tough sometimes. A dozen years into this and sometimes I don't even realize that's what's going wrong. The only advice I can give is to break things down into small pieces:

Code:
$ [command] | [command with regex]
  gArBaGe oUtPuT
Swear;

Code:
$ [command] > /tmp/am_i_sure_my_regex_is_being_fed_what_i_expect
$ vi /tmp/am_i_sure_my_regex_is_being_fed_what_i_expect
...then...
Code:
$ [command] | [one piece of my regex]
$ [command] | [different piece of my regex]
$ echo [sample of the text i really care about] | grep [simple regex]
$ echo [larger/different subset] | grep [gradually increasing regex]
You get the idea. And if the regex doesn't do what you want, it might be because of the shell. Stick
Code:
\
before characters in your regex that might be the cause. Sometimes the character causing the trouble is the escape character itself, so change that to
Code:
\\
.

And yeah, it all can look messy at first but it does get easier with practice. Look through some of the scripts under /etc and you'll find shell logic that looks like line noise; somebody wrote that...

Oh, one last thing: in shell world, 'single' and "double" quotes can make all the difference especially where this stuff is concerned. How so is left as an exercise for the already-bored reader.

Edit: ...Ha! This web app doesn't like backslashes either, it wiped mine out the first time I tried to post (even though they were in quotes)! See, nobody is immune from trouble with regex, even the pros who run this site. Big mojo.

Last edited by twork; 02-03-2016 at 07:27 PM.
 
Old 02-03-2016, 07:08 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,148

Rep: Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703Reputation: 4703
Quote:
Originally Posted by DavidMcCann View Post
Simple! I never use it.
Same here.

Last edited by dugan; 02-03-2016 at 07:11 PM.
 
Old 02-03-2016, 07:15 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,970

Rep: Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633Reputation: 3633
Couldn't survive without regex - I reckon I use it every day.
 
1 members found this post helpful.
Old 02-04-2016, 08:39 PM   #12
brockm789
LQ Newbie
 
Registered: Feb 2016
Posts: 17

Rep: Reputation: 3
I like using regex. They're useful with doing pattern matching/expansion. I use a lot of regex in my shell scripts. I'm not an expert on them, but I know enough to get the results I want.

I keep a journal of many regex expressions I've used and pickup on the net for reference.
 
2 members found this post helpful.
Old 02-05-2016, 04:22 AM   #13
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,964

Rep: Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798Reputation: 798
Quote:
Originally Posted by brockm789 View Post
I like using regex ...
I admit that regex is an essential part of Linux but I'm not sure the words 'like' and 'regex' belong in the same sentence!
 
Old 02-05-2016, 04:34 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,981

Rep: Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734Reputation: 5734
Quote:
Originally Posted by Keith Hedger View Post
I admit that regex is an essential part of Linux!
(obviously)
To survive: keep it simple! Sometimes I go to online regexp testers to see what am I really doing.
 
1 members found this post helpful.
Old 02-05-2016, 04:37 AM   #15
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by kaz2100 View Post
Question
How does everybody survive with regex?
By practicing. Not trying to be funny, if you USE regex you will learn. I agree that they are at times a pain in the b.tt, but they are also incredibly useful.

Best regards,
HMW
 
2 members found this post helpful.
  


Reply

Tags
awk, grep, perl, regex, 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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Help Linux survive cajunboy2k Linux - News 11 06-05-2006 10:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:15 AM.

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
Open Source Consulting | Domain Registration