Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
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.
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.
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 *)
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.
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)
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.