GoConqr has a new look! Let us know what you think.

Notes

REGEX - REGULAR EXPRESSIONS

Fen Pin English 8 pages

REGEX - REGULAR EXPRESSIONS

Content of the notes (8)

Page 1

LANGUAGE PCRE

From Perl, a bit more complex, but far more fast and performing than POSIX.

Fonctions (the more interesting ones) :

preg_grep

preg_split

preg_quote

preg_match

preg_match_all

preg_replace

preg_replace_callback

NB : All have their particularities, while they all use a similar syntax to do the researches.

E.G. : PREG_MATCH

return a boolean (ie true it there is a match with what we've searched)

it takes two args :

The regex (regular expression)

The chain where you want to search

I.E. : preg_match chain/file regex

Chain : I Love to play guitar

Regex : #guitar#

return true

be careful of CAPITALISATION, it count a lot!

I.E. : if the chain was Guitar it would have return false

NB : Not if you're regex was "#guitar#i" this option after the second diese tell the regex to not take care about capitalisation.

Page 2

SIMPLE SEARCHES

# : open and close the regex

| : or

^ : at beginning of the chain

$ : at end of the chain

CLASS OF CHARACTERS

SIMPLE CLASSES :

#gr[io]s# : between the bracket their is a class of characters which tells to the computer we're looking eather for the letter "i" or "o"

IE : #aeiouy$# is the regex for a chain

Interval of classes :

#[a-z]# : all min cap letter of the alphabeth (powerfull) /** unless I option 4 **/

#[A-Z]# : all MAJ cap letter of the alphabeth (powerfull) /*** ya follow ?***/

#[0-9]# : all digit numbers

Now guess what am I looking for if I type this insainely powerful regex [a-zA-Z0-9] ?

Tips

#[^0-9]# : .. won't tell all my whisperer's secret bitch (their untoldable btw.)

A GOOD EXEMPLE FOR THOSE WHO CAN'T SEE :)

(in french :/)

REGEX - REGULAR EXPRESSIONS

Page 3

Quantificator

Usual symbols :

? : mean this letter is facultative (it can be there 0 OR 1 time)

+ : mean this letter must be their, at least one time ( 0 OR ++ )

* : meant this letter could be their one time, many time (occurence) or not at all ( 0 OR 1 OR ++ )

("")* : mean those letters between parentheses can be repeated many times (i.e. : #ay(ay)*# => ayayayayya)

Bracket : Be precise like a ninja :supersayan:

The 3 ways of bracket-fu :

{nbr}: I simply put a number to the letter (or the group of letters between parentheses ;)

{nbr1,nbr2} : now we can have the letter (or the grp of ltr ;p) number of time equal to a number as nbr1 <= nbr of occurence <= nbr2

i.e. : #a{3,5}# > if i call grep_match with this regex as arg, it'll tell me :

for "..aaa.." , "..aaaa.." , "..aaaaa.." : true

for "a", "aa","aaaaaa" : false

{nbr, } : finaly we can have the grp of /ltr each number of time it is more or equal than the nbr :)

Page 4

METACHARACTER ? (Do it sound like it's Chinese ?)

# : indicate the end of the regex !

/!\ To use it you must antisash \ it, be very careful..

!

^

$ : indicate the end of a chain

(): makin grp of characters

{} : repetition of a pattern

[] : classes

] : /!\ To use it you must antisash \ it, be very careful..

? : facultatif

+ : include one time or more

* : not include, include once or more (anyway :)

. :

/!\ To use it you must antisash \ it, be very careful..

\ : useful :)

| : or

- : interval

/!\ To use it you must antisash \ it, be very careful..

it define an interval of class as [a-z] and, if you want add "-" in your search then you should type [a-z0-9-] or [-a-z0-9]

NB : they have no concrete signification for us but they are full of sense for the computer ;)

Why I need it though ?

Because, How could you search for a "What ?" ?

A moron, could eventualy type, as I did #What ?# > Guess what ? It's gat damn WRONG !!!

You'd rather type sthing like : #What \?# and actually you could chose the i option just in case of..

Page 5

Abreviated Classes

/!\ They're kind of shortcuts so it is very handy

\d : indicate that we're looking for a digit number

Same as typing [0-9]

\D : indicate that this NOT A DIGIT NUMBER

[^0-9]

\w : indicate an alphanumerque character or a "_"

[a-zA-Z0-9_]

\W : indique this is NOT an alphabetical character (or NOT a word)

[^a-zA-Z0-9_]

\t : indicate a tabulation

\n : indicate a new line

\r : indicate a return to the line

\s :indicate a white space

\S : indicate what is not a white space (\n; \t; \r)

. : indicate anycharacter (everything is athorised !)

Page 6

HOW TO : CONSTRUCT A COMPLETE REGEX 1/2

Phone NUMBER :

Firt number always a 0;

Second one is from 1 to 7;

The eight others can be any number!

> REGEX :

#^$#

#^0$#

#^0[1-7]$#

#^0[1-7][0-9]{8}$# | #^0[1-7]\d{8}$#

If number are like that : 06.11 | 06-11 | 061.1 .. :

Here is regex magic :

There is the 0 then the numbr between 1 and 7

Then there is either a space a tiret or a point > [-_. ]

/!\ but this is not an obligation .. > [-_. ]?

Then there there is two numbers > \d{2} | [0-9]{2}

Then we repeat this 4 times

You can make a poster with it : #^0[1-7]([-_. ]?[0-9]{2}){4}$# |

Page 7

HOW TO : MAKE A COMPLETE REGEX 2/2

For an E-mail adress (to test their validity) :

First we have the pseudo : Variable Chain of alphanumeric characters

> [a-zA-Z0-9._-]+ | > \w+

Secondly, We get the > @

Thirdly and finally, there is domain name : a long alphanumeric and a shorter alphabet chain of separated by a point

REGEX > [a-zA-Z0-9.-_]{2,}\.[a-z]{2,4} | > \w{2,}\.[a-z]{2,4}$

Page 8

CAPTURE & SUBSTITUTION with the magic "()"

Capturing Parenthese : "You can't imagine everythings parentheses could do for you :)"

Each () create a variable containing what it's actually embrace

I.E.: REGEX > #\[b\](.+)\[b\]#

WATCH OUT THE "\" before the bracket are only relevant in PHP !

MEANING : "FIND in the CHARACTER CHAIN a [b] FOLLOWED by ONE or MORE character, whatever they are FOLLOWED by [/b]"

The first variable is called ($1 and then $2 ..)

FUNCTION in PHP PREG_REPLACE :

ie : preg_replace('#\[b\](.+)\[b\]#','<strong>$1</strong>', @text

NB : If you don't want your parentheses to be capturing then you need it to start with ':'

I.E. : #(anti)co(?:nsti)(tu(tion)nelle)ment#

$0 : anticonstitutionnellement

$1: anti

$2 : tutionnelle

$3 :tion

Information

Description
Note on REGEX - REGULAR EXPRESSIONS, created by Fen Pin on 27/07/2017.
Created by
Fen Pin
Views
6
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?