How To Use Regular Expressions (Regex)

“Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.” – Jamie Zawinski

Every now and then I have some requirements where it entails parsing some data where I need to use some form of Regular Expression(regex), it is a syntax to use to search for patterns in a string or sets of strings. At first, the syntax looks intimidating and most people would shy away and resort to writing some functions to solve the issue.

Learning regex is a must-have skill to have as it can be applied to a wide range of tasks that needs some sort of search or parsing and is widely available in most programming languages, works on shells, text editors and IDEs, etc..). I use it most of the time when writing front end Javascript validation and backend logic in Apex NodeJS, Java/Groovy , Swift and Python

I created this cheat sheet that covers the basics and some handy tips.

Flags

The search pattern is normally delimited by two slash characters /abc/. At the end we can specify a combination of the following flags.

  • g (global) – Don’t return after the first match
  • m (multi-line) – ^ and $ match start/end of line
  • i (insensitive) case insensitive match
  • x (extended) ignore whitespace
  • X (eXtra) disallow meaningless escape
  • s (single line) dot matches new line
  • u (unicode) match with full unicode
  • U (Ungreedy) make quantifiers lazy
  • A (Anchored) anchor to start of pattern
  • J (Jchanged) allow duplicate subpattern names
  • D (Dollar end only) $ matches only end pattern


Anchors

Quantifiers

Or and Brackets

When inside bracket expressions all special character rules do not apply. E.g. \ to escape a character does not apply

Character Classes

In order to be taken literally, you must escape the characters ^.[$()|*+?{\with a backslash \ as they have special meaning.

Groupings

Greedy and Lazy Quantifiers

The quantifiers ( * + {}) are greedy operators, so they expand the match as far as they can through the provided text.

Boundaries

Back References – \1

Look-ahead and Look-behind

Top Regular Expressions

Summary

As we’ve learned regex is so powerful and its’s application is wide. Listed below some of few things you can do with regex within your project.

  • input and data validation –
    • validate user input in forms
    • validate data before applying logic or saving to database
    • validating JSON schema
  • replacing values – replace specific data in a string
  • text parsing – eg. retrieve only bits of data from a string or URL or delimiters
  • string replacement – eg on some IDE you can find and replace a string, use regex to search for particular patterns
  • web scraping – look for specific patterns for data to scrape

Sample Codes

Apex class that implements removal of white space not found in quotes

Python script that crawls pages that matches the pattern.

And finally, as a takeaway just learn the syntax and hack away.

How to Send Emails from WordPress on a Google Cloud Compute Instance

Google Compute Engine does not allow outbound connections on ports 25, 465 and 587. These SMTP ports are blocked by default due to abuse.

For sending emails through the Google cloud compute instance Google recommends the following:

  • Relay emails through your G suite(formerly known as Google Apps) account
  • Relay using third party email service such as Sendgrid,  Mailgun or Mailjet.
  • Connect your instance to your network via VPN and use your network to send email

With setting up the relay service we would need to configure and install Postfix. We won’t be installing and configuring Postfix on this tutorial. Instead we would be utilizing a WordPress WP Mail Plugin that would allow us to use a Gmail SMTP to send emails. I recommend setting up a new gmail account just for this purpose.

Read the rest of this entry »

How to Access phpMyAdmin on Google Cloud Compute on your Mac

I’ve been doing a small bit of Google Cloud Compute(GCP) configurations to run this blog. I thought I’ll start sharing some of the steps I’ve gone through which could be useful to others out there. So I’ve setup a GCP instance and configured it with a WordPress Bitnami stack. I’ve got some videos of those in this playlist if you want to follow along.

But on this post I would be discussing about accessing phpMyAdmin on GCP via an SSH Tunnel on your Mac.

In order to access phpMyadmin on a WordPress Bitnami stack from Google Cloud Compute you need to do this using an SSH tunnel. On the WordPress Bitnami stack, phpMyAdmin is blocked from the public and only accessible from the localhost. This is where SSH tunnel comes in. Basically you access the localhost on a particular port on your computer that forwards that information to the remote server on an encrypted channel and then the remote server sends the content back to the local computer.

Here is an illustration on how that access is provided via the SSH tunnel.

Another example of using SSH tunnel is when a website is blocked from your company firewall or proxy filter, you can use SSH tunnel to bypass the proxy and connect to a remote computer that has no restriction and can access the blocked website.

With that said, lets connect via SSH tunnel on your Mac.

  1. Open the terminal and change directory to where your private key is located
  2. Type in following, replace the private key name, user and IP to match yours. Enter passphrase when prompted.
    • ssh -N -L 8888:127.0.0.1:80 -i <keyfile> <username>@<ip>
  3. If successful it would not prompt any errors. If you want to login to the instance you can strip out the -N parameter.
    • ssh  -L 8888:127.0.0.1:80 -i <keyfile> <username>@<ip>

  4. Open your brower and enter 127.0.0.1:8888/phpMyAdmin
  5. You should be able to view phpMyAdmin

Here is a video I made for the process.

That should be it. If you have questions or comments please use the section below and feel free to subscribe to my youtube channel.

Recommended books:

Fixing Apache after upgrading to MacOS High Sierra

If your previous Apache setup stopped working after updating to MacOS High Sierra, then this post should be useful for you. On every update of the MacOS it ships with its own version of Apache and PHP and creates a new install that overrides your configuration.

Read the rest of this entry »

Free WordPress Qurtain Theme Version 0.1

After a long hiatus on developing on WordPress, I’m back. Finally, I have a fresh new look for the website with a new theme built from scratch. I’ve spent about roughly 30 hours building this theme. Relearning some basic and core CSS3, HTML5, and WordPress. I’ll be posting a video soon on how to build a responsive theme from scratch.

I intend to release this theme for free. It is in its early stage that has flaws but good enough to get it out there.

3 Quick Easy Steps To Speed Up your WordPress Site Now

Optimizing your WordPress powered website is a continuous process. It depends on how heavily customized your WordPress site is, luckily the out of the box the theme I installed ranked average.

Check your site at Think With Google or GtMetrix to get baseline of where your current website stands.

This is my baseline stats.

I started with these and with 3 quick steps and it took me less than 40 mins.

 

Step 1:  Enable gzip compression on your website. Edit the .htaccess file and add the following



# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml

# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent


Step 2: Go into your theme folder and grab the css and js files and make a backup. Minify the existing css files at Minifier.org and upload them back, if nothing breaks you are gold.

Step 3: Optimize and compress images on your site, install the Smush It plugin. Free edition lets you process 50 images at a time.

With this 3 simple steps you can quickly do to increase your site performance. Then you can go further and implement more methods like shop around for a better host, remove unnecessary plugins, start using CDNs where you host your images and javascript libraries or install a caching plugin like W3 Total Cache.

How to Install Apache Ant on a Mac

Created a video for installing apache ant on Mac running on MacOS Sierra and here is the steps to follow along.

  1. Download Apache Ant to your desktop and unzip the files
  2. Rename the folder to ant
  3. Open the Terminal
  4. Move the files to /usr/local

    [code]sudo mv ~/desktop/ant ~/usr/local/ant[/code]

  5. Fix the permission

    [code]chown -R root:wheel /usr/local/ant[/code]

  6. Edit the bash_profile with your favorite editor

    [code]sudo vi .bash_profile[/code]

  7. Add the path to the ant bin directory

    [code]export PATH=$PATH:/usr/local/ant/bin[/code]

  8. Save and exit your editor
  9. Test out if ant was successfully installed.

    [code]ant -v[/code]

 

Other Notes: If you are getting an error when you run any commands:

Unable to locate tools.jar. Expected to find it in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/tools.jar

This means you do not have the Java JDK installed. Go to java.com and download and install the JDK.

Edit your bash_profile and update the path for the JAVA_HOME.

[code]export JAVA_HOME=$(usr/libexec/java_home)[/code]

Subscribe to my Youtube channel for more how to video tutorials.

How to run on WordPress on a LEMP stack on Amazon EC2

At last, finally found time to move my blog on LEMP(Linux, Nginx, MySQL and PHP-FPM) stack and it just one of the many goals I have set for starting overhauling this blog and over the coming days I’ll be optimizing it further and come up with a fresh new design. I’ve been running this blog on Amazon EC2 LAMP stack on t1.micro instance. With micro instances you have limited processing power and memory. Running apache is kinda like an overkill, MySQL most often times crashes due to running out of memory and this layout design is outdated and not responsive. I’ve been focused too much on Salesforce development that I’ve completely snobbed this blog and first love which is designing, web development.

Here a short 3 part tutorial for setting up EC2, LEMP and WordPress

    Part 1 Setup EC2 instance

  • Sign up for AWS account
  • Create a new instance
  • Select Linux distro either Ubuntu 14.04 or higher (HVM preferred)
  • Select t2.micro instance and run through the wizard
  • Edit the security group and make sure you add SSH and HTTP rules
  • Then launch and download your key
  • Once launched get the public IP
  • On Mac SSH using pem key to the IP
    eg. sudo ssh -i my.pem ubuntu@1.1.1.1
  • Part 2 Setup Nginx,MySQL and PHP-FPM

  • Once connect make sure you update your distro local package (sudo apt-get update)
  • Next install nginx
    sudo apt-get install nginx
  • Install MySQL Server
    sudo apt-get install mysql-server
  • Setup MySQL structure
    sudo mysql_install_db
  • Run secure MySQL script and follow the prompts
    sudo mysql_secure_installation
  • Install PHP
    sudo apt-get install php5-fpm php5-mysql
  • Install PHP
    sudo apt-get install php5-fpm php5-mysql
  • Secure PHP
    sudo vi /etc/php5/fpm/php.ini
    Uncomment and set to cgi.fix_pathinfo=0
  • Restart PHP
    sudo service php5-fpm restart
  • Edit nginx configuration to read PHP
    sudo vi /etc/nginx/sites-available/default
  • Add index.php to be parsed
    index index.php index.html index.htm;
  • Restart nginx
    sudo service nginx restart
  • Setup server permissions
    sudo chown -R demo:www-data /var/www/html/*
    sudo chown -R www-data /var/www/wordpress
  • Sweet! Finally you can install your wordpress.

Configure WordPress Update on Ubuntu EC2 instance

EC2 instances uses SFTP and not FTP. Since you do not have a password. The easiest way to configure the WordPress update is via command line. SSH into your instance and enter the following.

sudo chown -R www-data /var/www/wordpress

sudo chmod -R 755 /var/www/wordpress

Forbidden You don’t have permission to access /~username/directory in Mac OS X

I have this weird issue with my website backups. When I store them inside my Sites folder and try to view them from the browser.

1. They do not appear listed when I do a directory listing
2. Accessing the folder manually produces the error

You don’t have permission to access on localhost /~username/mydirectory

This is well known and documented on Apple here.

But my issue is it is only a problem for this backups, the existing directories I have in my system works fine.

The solution is not straightforward but this works.

Follow the solution from Apple website. edit your username.conf file using terminal and then add the following line before closing.

Options +FollowSymLinks

So it looks like this.

<Directory “/Users/username/Sites/”>
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
Options +FollowSymLinks
</Directory>

Then restart apache

service apachectl restart