PHP REGEX: SETTING REGEX RULES

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to set regex rules

Requirements

  • WAMP/MAMP/LAMP/XAMP or any other PHP/MYSQL STACK

Difficulty

  • Intermediate

Tutorial Contents

A regular expression or regex is a sequence of characters that define a search pattern. It can be used by string searching algorithms for "find" or "find and replace" operations on strings.

Regex is not just in php, it is virtually being used in all programming languages. In this tutorial, I will be explaining how to set the regex rules in php.

In php, regex can be used via certain functions. This functions include :

  • preg_match()

Used for searching strings and returning true if a match is found and false if no match was found.

  • preg_match_all()

returns all the matches found in a string

  • preg_replace()

used for replacing a set of characters in a string

  • preg_split()

used for splitting a set of characters in a string

  • preg_grep()

Searches an array to find matching elements

  • preg_ quote()

takes a string and quotes all the special regular expression characters in it with a backslash

*NOTE: There are other functions that are not mentioned because they have been deprecated, so can not be found in the recent versions of php

This tutorial will need to first explain how to set the regex rules as each and every of this functions can not be used without setting the regex rules. Then in the next tutorial, we will talk about to use each of these functions.

Regex rules are stated like this below using preg_match as an example:
preg_match('/^steem/', 'steemit')

The part highlighted in red is the part of the function where the regex rules are stated. So this tutorial is basically explaining how to set this part while the next tutorial will explain how to use the different functions I stated earlier on.

  • [0-9] is used for matching all digits

  • [a-z] is used for matching lower case letters

  • [A-Z] is used for matching upper case letters

  • [a-Z] is used for matching both upper and lower case letters

Strings are also matched by the part of the string to be matched in the regex rule . For example \foo\ will definitely match the word foo because it has been stated directly in the rule

Note: Different rules can also be placed together as shown below:

For instance:[a-zA-Z0-9_] : This checks for Upper Case , lower case ,Numeric characters and underscores. Meaning this kind of regex will match a string like Love_Dove102 because it contains everything stated in the regex rule.

Apart from the above stated ways of stating regex matching rules, there are ways of matching the strings that can help improve the rules you can set.

  • ^ are used to state that a matching to start from the beginning.

e.g. /^fu/ will match futball but not kungfu because the fu needs to be at the beginning

  • $ are used to state that a matching to start from the end.

e.g. /$fu/ will match kungfu but not futball because the fu needs to be at the end

  • . will match any character between each other

e.g. /v.y/ will match velocity but not voltage because even thought voltage started with a v, it didnt end with a y

  • + is meant to match stated character one or more time.

e.g /ma+/ will still match mama or mamama even though it's stated one or more times

  • ? is used to make the string it follows un-compulsory.

e.g /donald?trump/ will still match donaldtrump and trump

  • * is used to make the string it follows un-compulsory but repetitive unlike ?

e.g /donald*trump/ will still match donaldtrump, donalddonaldtrump and trump

  • | is used to create allow the match of two different character string

e.g. /donald|trump/ will match either donald or trump

  • {} are used how many times a character can be matched.

e.g. /ade{1,} will match one or more of the last for instance, it would match /ade/ , /adee/ , /adeeee/ and so on.
v /ade{2,4,} will match one or more of the last for instance, it would match /adee/ , /adeee/ , /adeeee/ but not/ade/ or /adeeeee/

More so, there are letter like characters that can be used to set rules. they are listed below.

\d this matches any single digit like 8
\D this any single non digit character like a or 0=
\w this matches any alpha numeric character like letters,numbers,symbols and puntation mark including the underscore (_)
\W this matches any non alpha numeric character excluding the underscore character
\s this matches whitespace character meaning an empty space like

Using the preg_match() function as an example, let's try and match a string.

$string = 'steemit';
if (preg_match('/^steem/', $string){ echo 'true';} this will return true

Curriculum

Next in the curriculum is PHP REGEX, with this tutorial being the first.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

Like your contribution, upvote.

More ideas to make steem better.

Nice one dear,do i'm not a fan of all this coding stuff,it looks cool tho😄

Hey @akintunde I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 67328.17
ETH 2602.99
USDT 1.00
SBD 2.67