1. Skip to navigation
  2. Skip to content
  1. Skip to content
  2. Back to header

The Regular Expressions

Only just today have I realised the potential of Regular Expression – I’m no stranger to it, not by any means. I’ve used it for the usual search and replace, or finding something. But to my knowledge I have never used it in this capacity. I hope this article proves informative, regular expressions are worth learning about and getting familiar with, and this scenario has made me a fan for good.

My scenario is thus: I have written a number of lines of code, all using the same function and I’ve written them all the same with very little variability. For those curious I’m using PHP’s filter_var() function… which when I typed/copied/pasted these I was thinking in the same mindframe as using filter_input().

So my code looks a bit like this:


$v_name  =     filter_var('v_name', FILTER_SANITIZE_STRING);
$v_email =     filter_var('v_email', FILTER_VALIDATE_EMAIL|FILTER_SANITIZE_EMAIL);
$v_phone =     filter_var('v_phone', FILTER_SANITIZE_STRING);

As I’m pottering away today, I decide to myself – knowing this code could be wrong… to check the PHP documentation. Indeed, to little surprise it should infact be the variable instead of a string. No biggie, except by now these function calls are scattered throughout a page and in quite the number.

I decide to use regular expression, or RegEx. This should allow me to match specifics (like filter_var), and if I can find the right resources, store a specific part – that part being the actual name of the variable, remove the single quotes (by the power of ignoring) and replace them with a dollar sign.

Cue some Google-fu, I turned to the usual site regular-expression.info though it proves a bit flowery in language and wide (or specific) in scope; I’m hoping at this stage my choice of Aptana doesn’t hinder me. The wikipedia article is also feeling a bit obscure too. What I am looking for at this stage is how to store a specific match as a variable… and if regex even has this feature.

Finally, I strike gold – which states:

/(abc)/
Matches abc anywhere within the string, but the parentheses act as memory, storing abc in the variable $1.

What this means for me is that its possible, and I have to hope Aptana doesn’t have something I don’t know about which complicates matters. After a brief spot of experimenting (incidentally, GSkinner’s RegExr is a fantastic tool for this) I’m well on my way to tackling this.

Solution

Bare in mind that its worth making sure your regex works before committing to replacing; my find code eventually looked like this:


filter_var\('([a-z_]+)'

To break this down:

  • filter_var\(‘…’

This is the static part of the string, the function filter_var(, and the quotes ” the backslash ‘escapes’ the opening bracket (so its a part of what you’re looking for).

  • ([a-z_]+)

This is looking for characters within the range a-z, and underscores. Storing it using the parenthesis.

  • []+

The square brackets surround a group (above being a-z_), the + sign means multiples of the preceding group.

After making sure it works as expected, you want to replace (‘…’ with ($… – the code for replace is far simpler in my opinion:


filter_var\(\$$1

The backslashes you see dotted around ‘escape’ the following character, regular expression uses some characters, so needs to know when they’re a part of the expression, or a part of the string. Hence why the first dollar sign and opening parenthesis are escaped. Note the lack of single-quotes in the replacement expression and their presence in the find string.

If all goes well, the faulty lines of code are all replaced and should look something like this:

$v_name  =     filter_var($v_name, FILTER_SANITIZE_STRING);
$v_email =     filter_var($v_email, FILTER_VALIDATE_EMAIL|FILTER_SANITIZE_EMAIL);
$v_phone =     filter_var($v_phone, FILTER_SANITIZE_STRING);</pre>

Mine certainly did the trick!

Conclusion

While its debatable that it saved me much time right now thanks to the need for research, I’ve written this as a reminder to myself, as well as hopefully helping anyone else who might want to find out if regex can store matches as variables; or indeed the curious programmer who may not be aware of Regular Expression and its potential power.

In the future this kind of knowledge could save me a headache, and maybe even a good 20 minutes of repetitive correction; so I hope it helps you too – maybe not even for programming, most decent text editors have regular expressions as an option when finding things in a file (usually under advanced options).

No comments yet

Be the first!

Leave a Reply

Your email address will not be published. Required fields are marked *

Validation *

  1. Back to header
  2. Back to navigation
  3. Back to content

© 2011-2013 Tower Designs