Better Find and Replace with Regular Expressions
In this video you will see how you can use Regular Expressions (regex) when carrying out find and replace operations inside PHPStorm.
Now, if you're like me, you likely find the concept of Regular Expressions incredibly dull. But trust me, even that little bit of knowledge on regex can save you a ton of time. Let me give you an example:
On a very recent side project of mine, I ended up with a Doctrine entity that had over 40 different fields. The reasoning for this is a little odd, but it made sense for the proof-of-concept I was working on.
However, aside from having waaay too many fields, I also made another mistake. I set a bunch of
decimal fields to
scale's using string values, instead of integers. Something like this:
/** * @ORM\Column(name="some_field", type="decimal", scale="2", precision="8") */ private $someField;
And what I should have done was:
/** * @ORM\Column(name="some_field", type="decimal", scale=2, precision=8) */ private $someField;
And honestly, that's not that big of a deal if you just have one or two fields where you made this sort of mistake.
But I had a compounding issue.
I not only had a bunch of these fields I'd messed up (about 8 or 9), I had also switched a few of them round:
/** * @ORM\Column(name="another_field", type="decimal", precision="8", scale="3") */ private $anotherField;
Because sometimes, I am a bit of a chimp.
Anyway, rather than spend the 5 minutes to fix all this stuff by hand, I went off on a tangent to learn about how I could fix all this with one or two find / replace operations. Why spend 5 minutes when 20 minutes will do, eh?
But the end result, I think, is pretty cool. So I wanted to share it with you.
The first major benefit of using Regular Expressions over just a regular old find-and-replace is the concept of Matching Groups (also called Capturing Groups).
An example illustrates this better than words:
/** * @ORM\Column(name="some_field", type="decimal", scale="2", precision="8") */ private $someField; /** * @ORM\Column(name="some_field", type="decimal", scale="3", precision="8") */ private $anotherField;
Let's say we want to change both of these from
The first thing is - we can't do a basic find-and-replace as the
scale has a different number. This complicates things.
However, we can put the difference - the
2 or the
3 or whatever - into a Matching Group. We can then reference whatever was matched when doing the replace by using variables in the format of
$3 and so on, for as many groups as you matched / captured.
To do this in PHPStorm, we could do the standard find-and-replace (cmd / ctrl + r) and ensuring we tick the 'Regex' box, then we can change out pattern as follows:
And now when you do the Replace operation, you would end up with:
/** * @ORM\Column(name="some_field", type="decimal", scale=2, precision="8") */ private $someField; /** * @ORM\Column(name="some_field", type="decimal", scale=3, precision="8") */ private $anotherField;
Essentially, whatever is in the Matching Group is saved and placed into the replacement by way of the variable. Really handy.
Building Regular Expressions
The truth is, the syntax to regex is hard. Well, not so much hard as just incredibly confusing.
My secret is to use an online regular expression builder such as :
However, you really don't need to know that many of the different characters / symbols to start finding regex useful.
The main ones I use are:
.- Matches any single character except line breaks
?- Matches 0 or more of the preceeding token
+- Matches 1 or more of the preceeding token (see next example for clarity)
.+- Matches any single character except line breaks, one or more times (
^- beginning (usually, of a line - in my case)
$- end (usually, of a line - in my case)
$1- Matching Group, as above
.*?- Matches just about anything
Any combination of the above can usually get me where I need to be.
Another really nice feature of the regular expression builder links above is that they often have an 'explain' feature, which is not only useful for explaining just what the heck that regex you just created can do, but also, allows you to paste in existing regex to figure out someone else's voodoo.
Regex With PHPStorm
There's a handy reference guide on using Regular Expressions with PHPStorm in the official documentation.
Generally it's worth just having a quick play around with this now, so that when you do actually need this info in the future, it will be in your head as something you have seen and used in the past. It's not something you likely need every day, but when you do need it, it can be a helpful time saver.