Monday, April 8, 2013

Regular Expression di PHP (Regex di PHP)

Regular expression pada istilah komputer/pemrograman merupakan sebuah cara yang singkat dan fleksibel dalam mencari persamaan pada string, seperti mencari kata yang sama, kalimat yang sama atau mencari pola tertentu yang ada pada sebuah String.
Regular Expression (baca:selanjutnya di sebut dengan Regex saja) juga populer di kalangan UNIX user dalam melakukan beberapa operasi misalnya grep, dalam bahasa pemrograman seperti PHP Regex berguna untuk mempersingkan code dan mempercepat proses pencarian pada sekumpulan string.

Macam macam regular Expression di bahasa pemrograman PHP:

Operator dasar dalam membentuk regular expression.

Operator Description
^ The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$ Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
. The period matches any single character
? It will match the preceding pattern zero or one times
+ It will match the preceding pattern one or more times
* It will match the preceding pattern zero or more times
| Boolean OR
- Matches a range of elements
() Groups a different pattern elements together
[] Matches any single character between the square brackets
{min, max} It is used to match exact character counts
\d Matches any single digit
\D Matches any single non digit caharcter
\w Matches any alpha numeric character including underscore (_)
\W Matches any non alpha numeric character excluding the underscore character
\s Matches whitespace character

Dari table di atas dapat dicontohkan penggunaan Regex:

Example Description
‘/hello/’ It will match the word hello
‘/^hello/’ It will match hello at the start of a string. Possible matches are hello or helloworld, but not worldhello
‘/hello$/’ It will match hello at the end of a string.
‘/he.o/’ It will match any character between he and o. Possible matches are helo or heyo, but not hello
‘/he?llo/’ It will match either llo or hello
‘/hello+/’ It will match hello on or more time. E.g. hello or hellohello
‘/he*llo/’ Matches llo, hello or hehello, but not hellooo
‘/hello|world/’ It will either match the word hello or world
‘/(A-Z)/’ Using it with the hyphen character, this pattern will match every uppercase character from A to Z. E.g. A, B, C…
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the characters ab. E.g. matches abc, but not abcc
‘/abc{1,}/’ Matches one or more c character after the characters ab. E.g. matches abc or abcc
‘/abc{2,4}/’ Matches between two and four c character after the characters ab. E.g. matches abcc, abccc or abcccc, but not abc

Contoh Mudah Penggunaan Regex di PHP :

<h2>preg_filter([ekpresi], [pengganti], [target_string])</h2>
<?php
$subject = ‘hello kawanku! hello apa kabar ? sudah makan ? oh sudah’;
echo “<h3>Target 1 : \”<b>”.$subject.”</b>\”</h3>”;
echo “<h5>1.preg_filter rubah semua ‘hello’ jadi ‘hai’</h5>”;
print_r(preg_filter(‘/hello/’, ‘hai’, $subject));
echo “<h5>2.preg_filter ‘hello’ yang awal saja</h5>”;
print_r(preg_filter(‘/^hello/’, ‘hai’, $subject));
echo ‘<br />’;
echo “<h5>3.preg_filter rubah semua ‘sudah’ jadi ‘udah’</h5>”;
print_r(preg_filter(‘/sudah/’, ‘udah’, $subject));
echo “<h5>4.preg_filter rubah ‘sudah’ yang diakhir saja</h5>”;
print_r(preg_filter(‘/sudah$/’, ‘udah’, $subject));
echo “<h5>5.preg_filter cari kata yang diantara h-o, titik titik mennggambarkan jumlah huruf yang ditengah </h5>”;
print_r(preg_filter(‘/h…o/’, ‘Oiii’, $subject));
echo “<h5>6.Tambah Tanda Seru di Akhir </h5>”;
print_r(preg_filter(‘/$/’, ‘!’, $subject));
echo “<h5>6.Tambah Tanda Strip di Akhir </h5>”;
print_r(preg_filter(‘/^/’, ‘-’, $subject));
echo “<h5>6.Tambah Tanda Strip di Akhir </h5>”;
print_r(preg_filter(‘/^/’, ‘-’, $subject));
$subject = ‘hello kawanku! hello apa kabar ? sudah makan ? oh sudah, hebat’;
echo “<h3>Target 2 : \”<b>”.$subject.”</b>\”</h3>”;
echo “<h5>7.Mengganti sepotong </h5>”;
print_r(preg_filter(‘/he?/’, ‘Heish-’, $subject));
$subject = ‘hello kawanku! hello apa kabar ? sudah makan ? oh sudah, hebat hebat ‘;
echo “<h3>Target 3 : \”<b>”.$subject.”</b>\”</h3>”;
echo “<h5>8.mengganti kata  yang sama yang berulang2 </h5>”;
print_r(preg_filter(‘/hebat +/’, ‘super! ‘, $subject));
echo “<h5>9.mengganti kata  yang sama dari dua kondisi </h5>”;
print_r(preg_filter(‘/hello|hebat/’, ‘-hhhhh- ‘, $subject));
$subject = ‘hello HELLO kawanku! hello apa kabar ? sudah makan ? oh sudah, hebat hebat ‘;
echo “<h3>Target 4 : \”<b>”.$subject.”</b>\”</h3>”;
echo “<h5>9.mengganti dengan range huruf kecil  </h5>”;
print_r(preg_filter(‘/[a-e]/’, ‘x’, $subject));
echo “<h5>9.mengganti dengan range huruf  </h5>”;
print_r(preg_filter(‘/[A-E]/’, ‘x’, $subject));
echo “<h5>9.mengganti sesuai huruf  </h5>”;
print_r(preg_filter(‘/[EO]/’, ‘x’, $subject));
echo “<h5>9.mengganti sesuai range kombinasi besar kecil </h5>”;
print_r(preg_filter(‘/[a-eA-E]/’, ‘x’, $subject));
$subject = ‘hello HELLO kawanku! hello apa kabar ? sudah makan ? oh sudah, hebat hebat 12345678 ‘;
echo “<h3>Target 4 : \”<b>”.$subject.”</b>\”</h3>”;
echo “<h5>9.mengganti sesuai range kombinasi besar kecil dan angka </h5>”;
print_r(preg_filter(‘/[a-eA-E2-5]/’, ‘x’, $subject));
?>
<hr />
<h2>preg_grep ( $pattern , array $input [] ) </h2>
<?php
print_r( preg_grep (‘/[1-3]/’, array(‘mencoba 1234′, ’23423′, ‘huruf doang g muncul’, ‘huruf doang g muncul tapi di kasih angka 1 jadi muncul’ )) );
?>
<br />
Judul: Regular Expression di PHP (Regex di PHP); Ditulis oleh Unknown; Rating Blog: 5 dari 5

0 komentar:

Post a Comment