Understanding regular expressions can be hard and for a lot of people it seems too hard.
It’s not that hard and the benefits can be huge. Not to mention once learnt you will be astonished at where they can come in handy, it’s not just in
sed commands in your scripts. I often use it in my favourite text editor, BBEdit.
At the moment I’m working on extending
jss_tools so that it has coverage of the JSS data for mobile devices.
This requires building some large arrays containing the XML paths to the data and the dictionary keys used to store the data and building those arrays could be a tiresome task.
Instead I dump the XML into a file and then copy the bits of it I want into another text file before using the power of search and replace to turn that list into the array.
Given the XML, a find of
<([^>]*)>.* with a replace of
['\1', '\1'], gets me the array items. My final step is to go through and shorten some of the keys and I’m done. As you can see BBEdit grep compatible find is almost identical to the syntax used by
sed with the differeence that you don’t have to put a slash in front of the parentheses defining the portion of the string thst you will use in the replace.
When I need the array of keys and types to convert the JSS dazta strings into Python types then I can take that array and with a find of
^[^,]*, ('[^']*').* and a replace of
[\1, 'AAAA'], I’ve got it built to the point where I just go through and replace
AAAA with the type descriptor needed. The
AAAA makes it obvious which ones I haven’t set yet.
Oh, I always copy the text I’m working on into a fresh window. That way if I make some classic blunder I’ve always got the original. I know that “Undo” is there but I like working with a safety net as well.
Since I’m talking about BBEdit then there’s one other thing worth mentioning. To the right of the “Find” field in the Find & Replace dialog are two little icons. The clock icon will show your last 16 search and replace commands, the ‘g’ icon reveals a menu of search and replace commands with “Save…” and “Manage Patterns…” that you can use to build your own frequently used list. This is so neat that I’m often saving a search and replace that I might want later. By the way, if, like me, you want more than tbe last 16 search and replace commands then
defaults write com.barebones.bbedit FindAndReplaceHistorySize -int 32 on the command line will double the length.
When I taught my “Bash for Beginners” workshop at XWorld recently I made it a point to go through writing regular expressions in some detail and showed how you can write them by breaking the task down in to smaller bits. Mastering regular expressions is a skill useful in more places than just bash scripts, a good editor such as BBEdit that supports grep regular expressions and a bit of practice can save you huge amounts of time.
I first learnt regular expressions 35 years ago and you can imagine how much time I’ve saved since. Back thirty years ago I was a magazine editor and my boss and I developed some scripts in Nisus that used search and replace to perform things like take out two spaces after a period, replace ‘…’ with a proper ellipses and ‘–’ with a proper em-dash. That’s another advantage of doing something with search and replace, you can be sure that an automated process will pick up every instance while our all too human, do it by eye method will miss something.
I’ve used BBEdit for many years and I’m still learning to use it at it’s best. I encourage you to learn as much about your editor as possible, it’s a constant tool in your toolbox.