LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-05-2016, 12:14 PM   #16
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941

In actual practice, there are only a double-handful of regex features that are "incredibly useful," but those features are "incredibly useful!"

Okay, okay, it is "a language of chicken-scratches." That much is absolutely true.

Plus, when you are using regexes in shell scripts, you have to "backslash-escape" many symbols, such as periods and asterisks, so that the shell won't try to interpret them, itself.

It just takes a little getting used to.

However: if someone sees you using them, you must remember to drop your voice and to mutter phrases such as "Abracadabra." (Keeping a jar of newt's ears nearby is also a good idea, along with a jar of Orange Smoke.) No one must ever know that it is actually relatively easy to use!
 
Old 02-05-2016, 02:57 PM   #17
brockm789
LQ Newbie
 
Registered: Feb 2016
Posts: 17

Rep: Reputation: 3
Quote:
Originally Posted by Keith Hedger View Post
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!
It could've been worse. I could have said 'love' and 'regex'

On the serious side, most of my regex are simple ones. A few complex ones here and there, some regex I understand. I picked a lot regex expressions from commandlinefu.com.

Last edited by brockm789; 02-05-2016 at 02:58 PM.
 
1 members found this post helpful.
Old 02-05-2016, 03:14 PM   #18
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by brockm789 View Post
It could've been worse. I could have said 'love' and 'regex' ...
How revolting!
 
Old 02-05-2016, 03:20 PM   #19
brockm789
LQ Newbie
 
Registered: Feb 2016
Posts: 17

Rep: Reputation: 3
Quote:
Originally Posted by Keith Hedger View Post
How revolting!
I'm sorry, you feel that way :/

Peace
 
Old 02-05-2016, 03:25 PM   #20
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
22 Years in IT and I suck at RegEx.
I survive by asking those smarter than myself here, after I've tried a few examples on my own.

If I just spent about a week on it, it may even become un-spaghetti

5 minutes after this post, I'm sure I'll need help with my fail2ban regexes
 
Old 02-05-2016, 03:35 PM   #21
brockm789
LQ Newbie
 
Registered: Feb 2016
Posts: 17

Rep: Reputation: 3
Quote:
Originally Posted by Habitual View Post
22 Years in IT and I suck at RegEx.
I survive by asking those smarter than myself here, after I've tried a few examples on my own.

If I just spent about a week on it, it may even become un-spaghetti

5 minutes after this post, I'm sure I'll need help with my fail2ban regexes
I'm not an expert either. I started with the basics of regex and most of my scripts are using basic regex. It's almost impossible to decipher and remember those regex you see on the web. Sed and Awk deals with regex and have books dedicated to that subject.
 
Old 02-09-2016, 06:48 PM   #22
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,832

Original Poster
Rep: Reputation: 108Reputation: 108
Hya,

Now, I know that I am not the only one penguin who suffers from regex. Penguins can dive deep, so they can practice.

Shell escape and single/double quotes make things complicated together with '\|' and '|' in different versions of regex. (in addition to shell * and regex *)

Sigh,

cheers
 
Old 02-11-2016, 02:51 PM   #23
linuxteen
LQ Newbie
 
Registered: Feb 2016
Distribution: PclinuxOS 64bit && Crux linux 3.2 in a VM
Posts: 12

Rep: Reputation: Disabled
I'm not a fanatic of regular expressions. I know simple bash regular expressions. Like

rm *
ls *.[ma]*
ls file??.txt
cp file.txt{,.bak}

And maybe a few other bash regular expressions.

What's hard for me is the regular expressions used by sed, awk, python, perl and other scripting languages. I have to google it.
 
Old 02-16-2016, 11:10 PM   #24
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,832

Original Poster
Rep: Reputation: 108Reputation: 108
Hya

Yes, meta-notation is another headache.

Now, may be I know the difference between regex and glob (??shell file name substitution).

cheers
 
Old 04-10-2016, 11:50 PM   #25
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,832

Original Poster
Rep: Reputation: 108Reputation: 108
By the way, does anybody know how *? behaves in sed regex?

I was sure that it behaves lazy way, but it does not. The "?" looks like to behave as an ordinary char.

I need to figure out how *\? behaves.

cheers
 
Old 04-11-2016, 04:09 AM   #26
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be nice to give a full example (the command you want to understand)
 
Old 04-17-2016, 06:25 AM   #27
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,832

Original Poster
Rep: Reputation: 108Reputation: 108
Hya,

My motivation for post #25 was not on specific grammar or syntax. I am lost among deep confusion of regex sea.

Thank you very much for your listening to my SIGH.

cheers
 
Old 04-17-2016, 09:01 AM   #28
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by kaz2100 View Post
By the way, does anybody know how *? behaves in sed regex?

I was sure that it behaves lazy way, but it does not. The "?" looks like to behave as an ordinary char.

I need to figure out how *\? behaves.

cheers
In a basic regular expression, "?" is an ordinary character.

In "*\?" (or "*?" in an extended regular expression), the "\?" or "?" is basically a no-op. It makes the preceding atom optional, but the "*" already says it could appear zero or more times.

Here's an interesting variant:
Code:
sed -r -n '/abc{2,3}?xy/p'
From experiment, that is interpreted as meaning that the character "c" must appear 2 or 3 times or not at all, so the strings "abxy", "abccxy", and "abcccxy" match, but strings with other numbers of the character "c" do not.
 
Old 04-17-2016, 09:16 AM   #29
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
What I see is a bit different. see http://www.myezapp.com/apps/dev/regexp/show.ws and/or http://www.regexr.com/.
? will modify only the greediness.
Anyway I suggest to use these online regexp testers to understand how does it work (but it is not valid for sed, it has its own syntax, use sed -r instead)

I have tried the following /tmp/test:
Code:
pulse:x:110:119:Pulse?udio daemon,,,:/var/run/pulse:/bin/false
pulse:x:110:119:Pulse\udio daemon,,,:/var/run/pulse:/bin/false
puppet:x:120:129:Puppet configuration management daemon,,,:/var/lib/puppet:/bin/false
and executed the following:
Code:
sed -n '/^pu.*/p'    /tmp/test    # 3 lines returned
sed -n '/^pu.*?/p'   /tmp/test    # one line containing ? returned
sed -n '/^pu.*\\/p'  /tmp/test    # one line containing \ returned
sed -n '/^pu.*\?/p'  /tmp/test    # one line without \ and ? returned
sed -n '/^pu.*\\?/p' /tmp/test    # nothing returned
I cannot explain why

Last edited by pan64; 04-17-2016 at 09:50 AM.
 
Old 04-17-2016, 09:43 AM   #30
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pan64 View Post
What I see is a bit different. see http://www.myezapp.com/apps/dev/regexp/show.ws and/or http://www.regexr.com/t it
? will modify only the greediness.
Anyway I suggest to use these online regexp testers to understand how does it work (but it is not valid for sed, it has its own syntax, use sed -r instead)
Note that I did use "sed -r", and FWIW, "grep -E" behaves exactly the same way, regardless of what the online regexp testers say.
 
  


Reply

Tags
awk, grep, perl, regex, sed



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