Regex is the thing that you only learn when you need it. Unless you are processing a considerable amount of data, you likely wonβt use it.
Does t...
For further actions, you may consider blocking this person and/or reporting abuse
Am I the only one in the world who actually loves regular expressions? I learned them when I was studying about compilers and I always found them a very powerful tool, not necessarily for niche applications or very large amount of data.
I use them to build "tokenizers" or extract information from text files in just a couple of line of codes (in Ruby, mostly), for example (the first thing that came to my mind)
The syntax is not great, I agree, it looks much like line noise. I always wondered about an alternative syntax, but everything I tried (not much, to be honest) was not a really huge improvement.
Oh, yes, and let's not forget search-and-replace-regexp in emacs... You can do wonderful stuff with a single command.
This should be a great job working on regular expression all day. But it's not my cup of tea. I would prefer a mixing Regex with some sort of development.
Oh, is Emacs your favourite editor then? I can't image how good it will be to customise a replace operation using Regex.
Regex can be useful, but can also be a trap.
When you use regex, be sure to use just enough abstraction that you can swap out the regex implementation with a parser later on.
There are three main traps with regex:
It is quite difficult to predict when you'll hit one of these limits, so a little abstraction goes a long way.
Instead of putting regex directly in your code, abstract them with a procedure that does something: e.g., getName(foo) instead of (foo.match(/([^/]+)/) || [])[1]; :)
Great article! I've been using RegEx patterns a lot recently in Powershell as some commands return values as a very long string instead of a proper object. RegEx patterns make pulling the data much easier. I use RegEx 101 to help build my pattern strings. It has very helpful color coding and a dictionary of all the different RegEx operators.
Thank you, Catherine, for contributing to this article and providing the readers with addition resources. I wonder what you were using Regex for in PowerShell. Are you using Grep?
No, Powershell can use RegEx natively for working with strings. I mostly use
Select-String -Pattern
to pull substrings out of large string responses. Some string commands even use it by default and you have to remember that or else you'll be a bit confused why some of your code is not responding the way you hope.-split
and-replace
will use RegEx to match strings, but.Split()
and.Replace()
don't. So"catherine.mohan" -split "."
returns all the characters and("catherine.mohan").Split(".")
returnscatherine
andmohan
as expected.You can escape the period and it'll work too.
Is your file sparsely found everywhere that's why you are using command line? Often you will just use a program to do all these.
I'm not sure what file you're referring to. I use RegEx in the Powershell CLI, Powershell scripts, and in Powershell apps that I create. Mostly for parsing strings, and occasionally for searching strings. Powershell commands usually return objects with properties, but recently I've had to use some commands that return objects with a single property that is just a long string with all the values in a list Since I can't use the typical
$object.property
notation to get values, I have to use RegEx to parse the giant string looking for the values I need.Oh, using a string itself in PowerShell. Interesting. May I have more context about the application of it?
Sure! One of the recent times I've used regular expressions is when I needed to search the Windows Event Logs. In the GUI, you can only reliably search by Event ID even though the actual event has lots of info. You can get that info with the
Get-EventLog
Powershell command. It's all in the Message property, but that property is just a very long string even if it looks like this:Since I can't save it to a variable and access it like
$var.User
as you would expect, I have to do this instead to get the User value.If I need the same info from a lot of results, I will make arrays of my own custom objects so I only have to do the matching process once in a loop. Now that I can get the values, I can use them to filter the results and search for the events I need with greater accuracy.
Wow, that's so cool. I did not know that you could access EventLog through PowerShell. Thanks for sharing. I will try to explore interesting stuff you can do with PowerShell when I have time.
You make me laugh. It's good to have comments like that sometimes.
I think that I am a very frank guy. I always make things too funny that it appears easy, even if it's difficult.
Regex is invaluable for software dev and sysadmin. Just put in the effort to learn it - I guarantee it will be worth your time.
Try to tell that to the new people learning programming. All of them learning about web development to only create beautiful screen.
I suppose it all depends on your goals. Web development using $WEB_DEV_PLATFORM_DU_JOUR creates an initial perception of rapid progress. If you are one of us that is tasked with completing a complex project all the way to sustainable production, then traditional computer science concepts and tools become essential.
With rapid changing market, it is better to build a quick and dirty prototype. However, I do agree that a strong understanding of CS concepts is fundamental.
By the way, do you speak French? What with "DU_JOUR"?
"it is better to build a quick and dirty prototype'
Again, this depends on your goals. As a consultant I only get paid for a working product. If you are working as an employee, then the best strategy is to throw together the prototype, get some kudos, and move on to the next project.
Sadly I do not speak French. "du jour" is a French phrase adopted by English speakers for some time.
Pay attention, the first example is wrong:
/.+@.com/
matches something like "name@xcom". A better example (still not covering a lot of peculiar cases) could be/.+@.+\.com/
Thank you for pointing this mistake. I guess many people do not read thoroughly.
To me regex has always been regexr.com or regex101.com. π
I do an awful joke.
What is the difference between CoffeeScript and RegExp?
You can actually understand what the author of the code was going for when reading RegExp.
Hahaha this was a good one. As CoffeScript is trying to simply JavaScript, it gets hard to read sometimes.