126-Regular-Expressions-2, if search word is more than one , then it may return the result as per user defined

01- search (search p, h, ph differetly) character repetation in string
Array
(
    [0] => Array
        (
            [0] => Ph
            [1] => p
            [2] => p
            [3] => phhh
            [4] => p
            [5] => phhhhhh
            [6] => p
        )

)

02- above code example - 2
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 
            [2] => 
            [3] => 12
            [4] => 
            [5] => 
            [6] => 123
            [7] => 
            [8] => 
            [9] => 1234
            [10] => 
        )

)

03- select all digits with words
Array
(
    [0] => Array
        (
            [0] => word
            [1] => 1word
            [2] => 22word
            [3] => 333word
            [4] => 4444word
        )

)

04- only text with numbers
Array
(
    [0] => Array
        (
            [0] => 1word
            [1] => 22word
            [2] => 333word
            [3] => 4444word
        )

)

05- above example - 2, select all words
Array
(
    [0] => Array
        (
            [0] => file.php
            [1] => file1.php
            [2] => file23.php
            [3] => file456.php
        )

)

06- above example - 2, select all words with number only
Array
(
    [0] => Array
        (
            [0] => file1.php
            [1] => file23.php
            [2] => file456.php
        )

)

07- search oh with + Quantifiers
Array
(
    [0] => Array
        (
            [0] => oh
            [1] => ohhhh
            [2] => ohhhhhh
        )

)

08- search phone number with hypen between digits
Array
(
    [0] => Array
        (
            [0] => 212-456-7896
        )

)

09- search with p+ command
Array
(
    [0] => Array
        (
            [0] => P
            [1] => P
            [2] => p
            [3] => ppp
            [4] => p
        )

)

10- spelling middle part wrong but still want to fetch the details-1
Array
(
    [0] => Array
        (
            [0] => scipting
            [1] => scripting
        )

)

11- spelling middle part wrong but still want to fetch the details-2
Array
(
    [0] => Array
        (
            [0] => color
            [1] => colour
        )

)

12- serch using limiting, like select jan and january both from the string
Array
(
    [0] => Array
        (
            [0] => January
        )

    [1] => Array
        (
            [0] => uary
        )

)

13- serch above using string = Aug 22, Aug 22nd, August 22
Array
(
    [0] => Array
        (
            [0] => August 22nd
            [1] => Aug 22
            [2] => Aug 22nd
            [3] => August 22
        )

    [1] => Array
        (
            [0] => ust
            [1] => 
            [2] => 
            [3] => ust
        )

    [2] => Array
        (
            [0] => nd
            [1] => 
            [2] => nd
            [3] => 
        )

)

14- -------Limiting Repeatition-------
Array
(
    [0] => Array
        (
            [0] => hh
            [1] => hh
            [2] => hh
            [3] => hh
            [4] => hh
        )

)

15- --------------
Array
(
    [0] => Array
        (
        )

)

16- --------------
Array
(
    [0] => Array
        (
        )

)

17- -------salary in 6 digits-------
Array
(
    [0] => Array
        (
            [0] => 250000
            [1] => 152222
        )

)

18- -------------- -------
Array
(
    [0] => Array
        (
            [0] => PH
            [1] => ph
        )

)

19- -------------- -------
Array
(
    [0] => Array
        (
            [0] => scripting
        )

)

20- --Greedy And Lazy Match--------------- -------
Array
(
    [0] => Array
        (
            [0] => scripti
        )

)

21- -------------- -------
Array
(
    [0] => Array
        (
            [0] => scri
        )

)

22- -------------- -------
Array
(
    [0] => Array
        (
            [0] => 

PHP is the web scripting php language of choice.

) )

23- -------------- -------
Array
(
    [0] => Array
        (
            [0] => 

[1] =>

) )

24- -------------- -------
Array
(
    [0] => Array
        (
            [0] => 

PHP is the 'web' scripting 'php' language of choice.

) )

25- -------------- -------
Array
(
    [0] => Array
        (
            [0] => <
            [1] => h
            [2] => 1
            [3] => >
            [4] => P
            [5] => H
            [6] => P
            [7] =>  
            [8] => i
            [9] => s
            [10] =>  
            [11] => t
            [12] => h
            [13] => e
            [14] =>  
            [15] => '
            [16] => w
            [17] => e
            [18] => b
            [19] => '
            [20] =>  
            [21] => s
            [22] => c
            [23] => r
            [24] => i
            [25] => p
            [26] => t
            [27] => i
            [28] => n
            [29] => g
            [30] =>  
            [31] => '
            [32] => p
            [33] => h
            [34] => p
            [35] => '
            [36] =>  
            [37] => l
            [38] => a
            [39] => n
            [40] => g
            [41] => u
            [42] => a
            [43] => g
            [44] => e
            [45] =>  
            [46] => o
            [47] => f
            [48] =>  
            [49] => c
            [50] => h
            [51] => o
            [52] => i
            [53] => c
            [54] => e
            [55] => .
            [56] => <
            [57] => /
            [58] => h
            [59] => 1
            [60] => >
        )

)

26- -------------- -------
Array
(
    [0] => Array
        (
            [0] => P
        )

)

27- -------------- -------
Array
(
    [0] => Array
        (
            [0] => PHP
            [1] => is
            [2] => the
            [3] => web
            [4] => scripting
            [5] => ppphp
            [6] => language
            [7] => of
            [8] => choice
        )

)

27a- -------------- -------
Array
(
    [0] => Array
        (
            [0] => PHP
        )

)

28- -------------- -------
Array
(
    [0] => Array
        (
        )

)

29- -------------- -------
Array
(
    [0] => Array
        (
            [0] => .
        )

)

30- -------Grouping ????????????????????????-------
Array
(
    [0] => Array
        (
            [0] => possible
            [1] => impossible
        )

    [1] => Array
        (
            [0] => 
            [1] => im
        )

)

31- -----
Array
(
    [0] => Array
        (
            [0] => 212-456-7896
        )

    [1] => Array
        (
            [0] => 456
        )

    [2] => Array
        (
            [0] => 7896
        )

)

32- -----
Array
(
    [0] => Array
        (
            [0] => play
            [1] => player
            [2] => played
            [3] => playing
            [4] => playon
            [5] => play
        )

    [1] => Array
        (
            [0] => 
            [1] => er
            [2] => ed
            [3] => ing
            [4] => on
            [5] => 
        )

)

33- -----
Array
(
    [0] => Array
        (
            [0] => file.txt
            [1] => file1.xlsx
            [2] => file20.docx
            [3] => fileabc1.pptx
        )

    [1] => Array
        (
            [0] => txt
            [1] => xlsx
            [2] => docx
            [3] => pptx
        )

)

34- -----
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

)

35- -----
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

)

36- -----
Array
(
    [0] => Array
        (
            [0] => bill
            [1] => bill
        )

)

37- -----
Array
(
    [0] => Array
        (
            [0] => 100
        )

)

38- -----
Array
(
    [0] => Array
        (
            [0] => 100
        )

)

39- -----
Array
(
    [0] => Array
        (
            [0] => bill
            [1] => bill
        )

)

40- -----
Array
(
    [0] => Array
        (
            [0] => 250
            [1] => 900
            [2] => 856
        )

)

41- -----
Array
(
    [0] => Array
        (
            [0] => 100
            [1] => 900
        )

    [1] => Array
        (
            [0] => 
            [1] => 
        )

)

41a- ------- Assertion-------Positive Look behind ?<=-
Array
(
    [0] => Array
        (
            [0] => course
            [1] => course
        )

)

42- -----
Array
(
    [0] => Array
        (
            [0] => worker
        )

)

43- Assertion-------Negative Look behind
Array
(
    [0] => Array
        (
            [0] => worker
            [1] => worker
            [2] => worker
            [3] => worker
        )

)
<?php /* - 126-Regular-Expressions-2, if search word is more than one , then it may return the result as per user defined */ echo "</br><h7 class='myheading'>126-Regular-Expressions-2, if search word is more than one , then it may return the result as per user defined </h7></br>"; /*-------Repetition Quatifiers------- */ // 01- search (search p, h, ph differetly) character repetation in string echo "</br><span class='sh1'>01- search (search p, h, ph differetly) character repetation in string</span></br>"; $string01= "Php is the web scripting phhhp language 522 of choice phhhhhhp "; $exp01 = preg_match_all("/ph*/i", $string01, $array01); // search p, h, ph differetly echo "<pre>"; print_r($array01); echo "</pre>"; // 02- above code example - 2 echo "</br><span class='sh1'>02- above code example - 2</span></br>"; $string02= "1 12 123 1234"; $exp02 = preg_match_all("/\d*/i", $string02, $array02); echo "<pre>"; print_r($array02); echo "</pre>"; // 03- select all digits with words echo "</br><span class='sh1'>03- select all digits with words</span></br>"; $string03= "word 1word 22word 333word 4444word"; $exp03 = preg_match_all("/\d*word/i", $string03, $array03); //all digit before word echo "<pre>"; print_r($array03); echo "</pre>"; // 04- only text with numbers echo "</br><span class='sh1'>04- only text with numbers</span></br>"; $string04= "word 1word 22word 333word 4444word"; $exp04 = preg_match_all("/\d+word/i", $string04, $array04); // min one digit before word echo "<pre>"; print_r($array04); echo "</pre>"; // 05- above example - 2, select all words echo "</br><span class='sh1'>05- above example - 2, select all words</span></br>"; $string05= "file.php file1.php file23.php file456.php"; $exp05 = preg_match_all("/file\d*.php/i", $string05, $array05); echo "<pre>"; print_r($array05); echo "</pre>"; // 06- above example - 2, select all words with number only echo "</br><span class='sh1'>06- above example - 2, select all words with number only</span></br>"; $string06= "file.php file1.php file23.php file456.php"; $exp06 = preg_match_all("/file\d+.php/i", $string06, $array06); echo "<pre>"; print_r($array06); echo "</pre>"; // 07- search oh with + Quantifiers echo "</br><span class='sh1'>07- search oh with + Quantifiers</span></br>"; $string07= "o oh ohhhh ohhhhhh"; $exp07 = preg_match_all("/oh+/i", $string07, $array07); echo "<pre>"; print_r($array07); echo "</pre>"; // 08- search phone number with hypen between digits echo "</br><span class='sh1'>08- search phone number with hypen between digits</span></br>"; $string08= "212-456-7896 212*456*7896"; $exp08 = preg_match_all("/\d+[-]\d+[-]\d+/i", $string08, $array08); echo "<pre>"; print_r($array08); echo "</pre>"; // 09- search with p+ command echo "</br><span class='sh1'>09- search with p+ command</span></br>"; $string09= "PHP is the web scripting ppphp language 522 of choice."; $exp09 = preg_match_all("/p+/i", $string09, $array09); echo "<pre>"; print_r($array09); echo "</pre>"; /* 10- spelling middle part wrong but still want to fetch the details-1 */ echo "</br><span class='sh1'>10- spelling middle part wrong but still want to fetch the details-1</span></br>"; $string10= "PHP is the web scipting ppphp scripting language of choice."; $exp10 = preg_match_all("/scr?ipting/i", $string10, $array10); //-- if "r" is doubt/optional is present or not echo "<pre>"; print_r($array10); echo "</pre>"; /* 11- spelling middle part wrong but still want to fetch the details-2 */ echo "</br><span class='sh1'>11- spelling middle part wrong but still want to fetch the details-2</span></br>"; $string11 = "color colour"; $exp11 = preg_match_all("/colou?r/i", $string11, $array11); echo "<pre>"; print_r($array11); echo "</pre>"; /* 12- serch using limiting, like select jan and january both from the string */ echo "</br><span class='sh1'>12- serch using limiting, like select jan and january both from the string </span></br>"; $string12= "Jan January"; $exp12 = preg_match_all("/Jan(uary)/i", $string12, $array12); //<--- (uary) is optional echo "<pre>"; print_r($array12); echo "</pre>"; /* 13- serch above using string = Aug 22, Aug 22nd, August 22 */ echo "</br><span class='sh1'>13- serch above using string = Aug 22, Aug 22nd, August 22 </span></br>"; $string13= "August 22nd Aug 22 Aug 22nd August 22"; $exp13 = preg_match_all("/Aug(ust)? 22(nd)?/i", $string13, $array13); echo "<pre>"; print_r($array13); echo "</pre>"; /* 14- -------Limiting Repeatition------- */ echo "</br><span class='sh1'>14- -------Limiting Repeatition------- </span></br>"; $string14= "o oh ohhhh ohhhhhh"; $exp14 = preg_match_all("/h{2}/i", $string14, $array14); echo "<pre>"; print_r($array14); echo "</pre>"; /* 15- -------Limiting Repeatition------- */ echo "</br><span class='sh1'>15- -------------- </span></br>"; $string15= "o oh ohhhh ohhhhhh"; $exp15 = preg_match_all("/ph{2,}/i", $string15, $array15); //--- minimum char "h" is 2 time together echo "<pre>"; print_r($array15); echo "</pre>"; /* 16- -------Limiting Repeatition------- */ echo "</br><span class='sh1'>16- -------------- </span></br>"; $string16= "o oh ohhhh ohhhhhh"; $exp16 = preg_match_all("/ph{2,2}/i", $string16, $array16); echo "<pre>"; print_r($array16); echo "</pre>"; /* 17- -------salary in 6 digits------- ------- */ echo "</br><span class='sh1'>17- -------salary in 6 digits-------</span></br>"; $string17= "12000 250000 45820 152222"; $exp17 = preg_match_all("/\d{6,}/i", $string17, $array17); echo "<pre>"; print_r($array17); echo "</pre>"; /* 18- -------------- ------- */ echo "</br><span class='sh1'>18- -------------- -------</span></br>"; $string18= "PHP is the web scripting ppphp language of choice."; $exp = preg_match_all("/ph{1,}/i", $string18, $array18); //--- minimum char "h" is 1 after "p" echo "<pre>"; print_r($array18); echo "</pre>"; /* 19- -------------- ------- */ echo "</br><span class='sh1'>19- -------------- -------</span></br>"; $string19= "PHP is the web scripting ppphp language of choice."; $exp19 = preg_match_all("/s[a-zA-Z]{2,}/", $string19, $array19); //--- Not Clear echo "<pre>"; print_r($array19); echo "</pre>"; /* 20- -------------- ------- */ echo "</br><span class='sh1'>20- --Greedy And Lazy Match--------------- -------</span></br>"; $string20= "PHP is the web scripting ppphp language of choice."; $exp20 = preg_match_all("/s[a-z]*i/", $string20, $array20); //--- Greedy Match or use (+) echo "<pre>"; print_r($array20); echo "</pre>"; /* 21- -------------- ------- */ echo "</br><span class='sh1'>21- -------------- -------</span></br>"; $string21= "PHP is the web scripting ppphp language of choice."; $exp21 = preg_match_all("/s[a-z]*?i/", $string21, $array21); //--- Lazy Match echo "<pre>"; print_r($array21); echo "</pre>"; /* 22- -------------- ------- */ echo "</br><span class='sh1'>22- -------------- -------</span></br>"; $string22= "<h1>PHP is the web scripting php language of choice.</h1>"; $exp22 = preg_match_all("/<.*>/", $string22, $array22); //--- Greedy Match or use (+) echo "<pre>"; print_r($array22); echo "</pre>"; /* 23- -------------- ------- */ echo "</br><span class='sh1'>23- -------------- -------</span></br>"; $string23= "<h1>PHP is the web scripting php language of choice.</h1>"; $exp23 = preg_match_all("/<.*?>/", $string23, $array23); //--- Lazy Match echo "<pre>"; print_r($array23); echo "</pre>"; /* 24- -------------- ------- */ echo "</br><span class='sh1'>24- -------------- -------</span></br>"; $string24= "<h1>PHP is the 'web' scripting 'php' language of choice.</h1>"; $exp24 = preg_match_all("/.+/", $string24, $array24); //--- Greedy Match echo "<pre>"; print_r($array24); echo "</pre>"; /* 25- -------------- ------- */ echo "</br><span class='sh1'>25- -------------- -------</span></br>"; $string25= "<h1>PHP is the 'web' scripting 'php' language of choice.</h1>"; $exp25 = preg_match_all("/.+?/", $string25, $array25); //--- Lazy Match echo "<pre>"; print_r($array25); echo "</pre>"; /* 26- -------------- ------- */ echo "</br><span class='sh1'>26- -------------- -------</span></br>"; $string26= "PHP is the web scripting ppphp language of choice."; $exp26 = preg_match_all("/^p/i", $string26, $array26); //--- starting with echo "<pre>"; print_r($array26); echo "</pre>"; /* 27- -------------- ------- */ echo "</br><span class='sh1'>27- -------------- -------</span></br>"; $string27= "PHP is the web scripting ppphp language of choice."; $exp27 = preg_match_all("/\w+/i", $string27, $array27); echo "<pre>"; print_r($array27); echo "</pre>"; /* 27a- -------------- ------- */ echo "</br><span class='sh1'>27a- -------------- -------</span></br>"; $string27a= "PHP is the web scripting ppphp language of choice."; $exp27a = preg_match_all("/^\w+/i", $string27a, $array27a); //----select first word only echo "<pre>"; print_r($array27a); echo "</pre>"; /* 28- -------------- ------- */ echo "</br><span class='sh1'>28- -------------- -------</span></br>"; $string28= "PHP is the web scripting ppphp language of choice."; $exp28 = preg_match_all("/^\w+$/i", $string28, $array28); //----select only last word echo "<pre>"; print_r($array28); echo "</pre>"; /* 29- -------------- ------- */ echo "</br><span class='sh1'>29- -------------- -------</span></br>"; $string29= "PHP is the web scripting ppphp language of choice."; $exp29 = preg_match_all("/.$/i", $string29, $array29); //--- end with echo "<pre>"; print_r($array29); echo "</pre>"; /* 30- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>30- -------Grouping ????????????????????????-------</span></br>"; $string30= "possible impossible"; $exp30 = preg_match_all("/(im)?possible/", $string30, $array30); echo "<pre>"; print_r($array30); echo "</pre>"; /* 31- -------------- */ echo "</br><span class='sh1'>31- -----</span></br>"; $string31= "212-456-7896"; $exp31 = preg_match_all("/\d{3}-(\d{3})-(\d{4})/", $string31, $array31); echo "<pre>"; print_r($array31); echo "</pre>"; /* 32- -------????????????????????????------- */ echo "</br><span class='sh1'>32- -----</span></br>"; $string32= "play player played playing playon play123"; $exp32 = preg_match_all("/play([a-z]+)?/", $string32, $array32); echo "<pre>"; print_r($array32); echo "</pre>"; /* 33- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>33- -----</span></br>"; $string33= "file.txt file1.xlsx file20.docx fileabc1.pptx"; $exp33 = preg_match_all("/file\w*\.(txt|xlsx|docx|pptx)/", $string33, $array33); echo "<pre>"; print_r($array33); echo "</pre>"; /* 34- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>34- -----</span></br>"; $string34= "I like Toyota and Honda I like Toyota and Honda and Toyota I like Toyota and Honda and Honda"; $exp34 = preg_match_all("/I like (Toyota) and (Honda) and \1/", $string34, $array34); //<--- Targeting Group 1 echo "<pre>"; print_r($array34); echo "</pre>"; /* 35- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>35- -----</span></br>"; $string35= "I like Toyota and Honda I like Toyota and Honda and Toyota I like Toyota and Honda and Honda"; $exp35 = preg_match_all("/I like (Toyota) and (Honda) and \2/", $string35, $array35); //<--- Targeting Group 2 echo "<pre>"; print_r($array35); echo "</pre>"; /* 36- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>36- -----</span></br>"; /*------- Assertion-------?=n <--- Positive Lookahead------- */ $string36= "bill paid bill not paid bill paid bill not paid"; $exp36 = preg_match_all("/bill(?=\spaid)/", $string36, $array36); echo "<pre>"; print_r($array36); echo "</pre>"; /* 37- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>37- -----</span></br>"; $string37= "100 USD 250 JPY 900 EUR 856 INR "; //******************************** Continue FROM HERE $exp37 = preg_match_all("/\d{3}(?=\sUSD)/", $string37, $array37); echo "<pre>"; print_r($array37); echo "</pre>"; /* 38- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>38- -----</span></br>"; $string38= "100 USD 250 JPY 900 EUR 856 INR "; //******************************** Continue FROM HERE $exp38 = preg_match_all("/\d+(?=\sUSD)/", $string38, $array38); echo "<pre>"; print_r($array38); echo "</pre>"; /* 39- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>39- -----</span></br>"; /*------- Assertion-------?!n <--- Negative Look Ahead------- */ $string39= "bill paid bill not paid bill paid bill not paid"; $exp39 = preg_match_all("/bill(?!\spaid)/", $string39, $array39); //<-- select bill whom after not coming paid echo "<pre>"; print_r($array39); echo "</pre>"; /* 40- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>40- -----</span></br>"; $string40= "100 USD 250 JPY 900 EUR 856 INR "; //******************************** Continue FROM HERE $exp40 = preg_match_all("/\d{3}(?!\sUSD)/", $string40, $array40); //<-- select currencies other than USD --- \d+(?=\sUSD) echo "<pre>"; print_r($array40); echo "</pre>"; /* 41- -------????????????????????????------- */ echo "</br><span class='sh1'>41- -----</span></br>"; $string41= "100 USD 250 JPY 900 EUR 856 INR "; //******************************** Continue FROM HERE $exp41 = preg_match_all("/\d{3}(?!\s(JPY|INR))/", $string41, $array41); echo "<pre>"; print_r($array41); echo "</pre>"; /* 41a ------- Assertion-------Positive Look behind ?<=- ------- */ echo "</br><span class='sh1'>41a- ------- Assertion-------Positive Look behind ?<=- </span></br>"; $string= "free course paid course free course paid course"; $exp = preg_match_all("/(?<=free\s)course/", $string, $array); //<--- select only course which will come after free word echo "<pre>"; print_r($array); echo "</pre>"; /* 42- -------Grouping ????????????????????????------- */ echo "</br><span class='sh1'>42- -----</span></br>"; $string42= "social worker hard worker lazy worker poor worker intelligent worker"; $exp42 = preg_match_all("/(?<=social\s)worker/", $string42, $array42); echo "<pre>"; print_r($array42); echo "</pre>"; /* 43- ------- Assertion-------Negative Look behind------- */ echo "</br><span class='sh1'>43- Assertion-------Negative Look behind</span></br>"; $string43= "social worker hard worker lazy worker poor worker intelligent worker"; $exp43 = preg_match_all("/(?<!social\s)worker/", $string43, $array43); //--- all except social echo "<pre>"; print_r($array43); echo "</pre>"; ?>