Thursday, September 10, 2015

How to Getting start with regular expressions


Regular expressions are a very useful tool for developers. They allow to find, identify or replace text, words or any kind of characters. In this article, I have compiled 15+ extremely useful regular expressions that any web developer should have in his toolkit.

Getting started with regular expressions
For many beginners, regular expressions seems to be hard to learn and use. In fact, they’re far less hard than you may think. Before we dive deep inside regexp with useful and reusable codes, let’s quickly see the basics:
Regular expressions syntax
Regular Expression
Will match…
foo
The string “foo”
^foo
“foo” at the start of a string
foo$
“foo” at the end of a string
^foo$
“foo” when it is alone on a string
[abc]
a, b, or c
[a-z]
Any lowercase letter
[^A-Z]
Any character that is not a uppercase letter
(gif|jpg)
Matches either “gif” or “jpeg”
[a-z]+
One or more lowercase letters
[0-9.-]
Аny number, dot, or minus sign
^[a-zA-Z0-9_]{1,}$
Any word of at least one letter, number or _
([wx])([yz])
wy, wz, xy, or xz
[^A-Za-z0-9]
Any symbol (not a number or a letter)
([A-Z]{3}|[0-9]{4})
Matches three letters or four numbers

PHP regular expression functions
Function
Description
preg_match()
The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise.
preg_match_all()
The preg_match_all() function matches all occurrences of pattern in string.
preg_replace()
The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_split()
The preg_split() function operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.
preg_grep()
The preg_grep() function searches all elements of input_array, returning all elements matching the regexp pattern.
preg_ quote()
Quote regular expression characters

Validate domain name
Verify if a string is a valid domain name.


3 comments:

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide

How to Host a Laravel Project on Hostinger’s hPanel: A Step-by-Step Guide If you're looking to host a Laravel project on Hostinger’s hPa...