+ - Matches any single character except line breaks, one or more times ( +) + - Matches 1 or more of the preceeding token (see next example for clarity).? - Matches 0 or more of the preceeding token.Matches any single character except line breaks However, you really don't need to know that many of the different characters / symbols to start finding regex useful. Or if you are on Windows, RegexBuddy is really nice (paid).My secret is to use an online regular expression builder such as : Well, not so much hard as just incredibly confusing. The truth is, the syntax to regex is hard. * type="decimal", scale=3, precision="8")Įssentially, whatever is in the Matching Group is saved and placed into the replacement by way of the variable. * type="decimal", scale=2, precision="8") 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:Īnd now when you do the Replace operation, you would end up with: /** We can then reference whatever was matched when doing the replace by using variables in the format of $1, $2, $3 and so on, for as many groups as you matched / captured. However, we can put the difference - the 2 or the 3 or whatever - into a Matching Group. The first thing is - we can't do a basic find-and-replace as the scale has a different number. Let's say we want to change both of these from scale=". 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).Īn example illustrates this better than words: /** Why spend 5 minutes when 20 minutes will do, eh?īut the end result, I think, is pretty cool. * type="decimal", precision="8", scale="3")īecause sometimes, I am a bit of a chimp.Īnyway, 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. 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: /** * type="decimal", scale="2", precision="8")Īnd honestly, that's not that big of a deal if you just have one or two fields where you made this sort of mistake. I set a bunch of decimal fields to precision's and scale's using string values, instead of integers. However, aside from having waaay too many fields, I also made another mistake. The reasoning for this is a little odd, but it made sense for the proof-of-concept I was working on. On a very recent side project of mine, I ended up with a Doctrine entity that had over 40 different fields. But trust me, even that little bit of knowledge on regex can save you a ton of time. Now, if you're like me, you likely find the concept of Regular Expressions incredibly dull. In this video you will see how you can use Regular Expressions (regex) when carrying out find and replace operations inside PHPStorm.