preg-grep

permutation using regular expression in php

让人想犯罪 __ 提交于 2019-12-11 12:18:56
问题 Is there any way to get permutation of all words in a string using only regular expression in php. For example: for input like "How to remove pain in human knee" I want output as: "How To", "How","pain knee","remove knee","knee pain","in remove","human pain knee", etc. 回答1: Using regex will only serve to slow down the process. I have written a function based on the powerSet() function posted by Yada @ https://stackoverflow.com/a/27968556/2943403) Code: (Demo) function permStacker($array){

Regular expression to match IP addresses

不问归期 提交于 2019-12-11 10:53:17
问题 I have file.txt which contains this data: livebox.home (192.168.1.1) set-top-box41.home (192.168.1.10) pc38.home (192.168.1.11) pc43.home (192.168.1.12) pc39.home (192.168.1.15) I want to extract the IP of pc39.home . I have used this regular expression but it not work: preg_grep("#^[a-z]{2}39.[a-z]{4} \d{3}\.\d{3}\.\d{1}\.\d{2}#",$myfile); The result should be 192.168.1.15 . 回答1: with this you can just change the $search if you want to caputure any of the other IPs. $search = "pc39\.home";

Find character before or after of string using preg_grep() in PHP

妖精的绣舞 提交于 2019-12-02 19:06:22
问题 I need to find any character before or after of a word using PHP preg_grep() from array. I have a array like following $findgroup = array("aphp", "phpb", "dephpfs", "potatoes"); I need to find the values from the array which have 'php' word with a single or double character before or after(either side, not both side) the word 'php'. The result should be 'aphp','phpb' word from the array. I tried with the following code but not works. $result[] = preg_grep("/(.{1})php(.{1})/", $findgroup); 回答1