preg-match

Regex to match alphanumeric characters, underscore, periods and dash, allowing dot and dash only in the middle

不打扰是莪最后的温柔 提交于 2019-12-21 17:35:45
问题 Presently, I am using this: if (preg_match ('/^[a-zA-Z0-9_]+([a-zA-Z0-9_]*[.-]?[a-zA-Z0-9_]*)*[a-zA-Z0-9_]+$/', $product) ) { return true; } else { return false } For example, I want to match: pro.duct-name_ _pro.duct.name p.r.o.d_u_c_t.n-a-m-e But I don't want to match: pro..ductname .productname- -productname. -productname 回答1: The answer would be /^[a-zA-Z0-9_]+([-.][a-zA-Z0-9_]+)*$/ if only you allowed strings containing .- and -. NOT to match. Why would you allow them to match, anyway?

Is it safe to use user's RegEx?

人走茶凉 提交于 2019-12-21 07:01:26
问题 I want to add a feature to my website to let users search the texts with RegEx . But, is it safe to let the users do something like that ? preg_match('/' . $user_input_regex . '/', $subject); 回答1: There is a possible attack on this code called a ReDoS attack (Regular expression Denial of Service). The Regular expression Denial of Service (ReDoS) is a Denial of Service attack, that exploits the fact that most Regular Expression implementations may reach extreme situations that cause them to

Maximum Hex value in regex

╄→尐↘猪︶ㄣ 提交于 2019-12-21 03:18:29
问题 Without using u flag the hex range that can be used is [\x{00}-\x{ff}] , but with u flag it goes up to a 4-byte value \x{7fffffff} ( [\x{00000000}-\x{7fffffff}] ). So if I execute the below code: preg_match("/[\x{00000000}-\x{80000000}]+/u", $str, $match); Will get this error : Warning: preg_match(): Compilation failed: character value in \x{...} sequence is too large So I can't match a letter like 𡃁 with equivalent hex value of f0 a1 83 81 . The question is not how to match these letters,

how can i use preg_match with alphanumeric and unicode acceptance?

纵然是瞬间 提交于 2019-12-21 03:12:18
问题 I am going to build a multilingual website with PHP and need to have a preg_match which passes all Unicode characters and numbers. i.e I need it to pass English letters, Spanish letters,Italian letters and as you may know I don't want to pass other characters like ' " _ - and ... I want some thing like this : $pattern='/^[unicode chars without \'-_;?]*$/u'; if(!preg_match($pattern, $url)) echo #error; 回答1: Unicode property for letter is \pL so in preg_match : preg_match('/^\pL+$/u', $string);

Regex pattern needed

孤者浪人 提交于 2019-12-20 07:58:43
问题 If have below text: <script> LMS.pageData['product']['percentageDiscount'] ='26'; LMS.pageData['product']['newPrice'] ='37'; LMS.pageData['product']['oldPrice'] ='50.0'; LMS.pageData['product']['savedAmount'] ='13'; LMS.pageData['product']['price']['value'] = '37'; </script> Then I need to capture newPrice and oldPrice values, please help me for Regex pattern. thanks! 回答1: You can use the following to match: \['newPrice'\] ='([\d.]*)'|\['oldPrice'\] ='([\d.]*)'|\['price'\]\['value'\] = '([\d.

Regex to match a word, even is there are spaces between letters

喜夏-厌秋 提交于 2019-12-20 07:47:56
问题 I'd like to have a regex to match a word, even if there are spaces between the characters. When I want to match the word test , it should match the following: test t est t e s t And so on, but it should not match things like this: tste te ts s tet I have this regex: (t[\s]*e[\s]*s[\s]*t[\s]*) But I don't believe that this one is very efficient. 回答1: Actually, it is the same as t\s*e\s*s\s*t (if the word appears inside a larger string, \bt\s*e\s*s\s*t\b is preferable). This is the only way to

PHP Regex to match multiline

家住魔仙堡 提交于 2019-12-20 07:22:27
问题 i need to build a pattern to match a multiline code $data['infos'] = array(); foreach ($infos as $info) { $data['infos'][] = array( 'title' => $special['title'], 'text' => $special['summary'],, ); } $data['next'] = $this->url('page/next'); $data['page'] = $this->url('page/curent'); my attempt was to use (\$data\['infos']\[] = array\(\n).*\n\t.*\n\t*\)\; but it did not work, but when i tried (\$data\['infos']\[] = array\() only this part was match $data['infos'][] = array( how do i build the

preg_match for nested html tags

这一生的挚爱 提交于 2019-12-20 06:36:37
问题 I would like to catch all "dev" tags and their respective content, through php preg_match_all() but can't get the nested ones. data: <dev>aaa</dev> <dev>bbb</dev> <dev> ccc <dev>ddd</dev> </dev> my expression so far: |<dev>(.*)</dev>|Uis thanks, for your help, b. 回答1: Don’t use regular expressions for parsing. Use a real parser like DOMDocument or SimpleXML: $xml = simplexml_load_string('<root>'.$str.'</root>'); 回答2: You need to have a recursive matching pattern: /<dev>(.*|(?R))<\/dev>/i That

preg_match() fails with string containing slashes

こ雲淡風輕ζ 提交于 2019-12-20 05:38:30
问题 I have a function like this: function in_array_r($item , $array){ return preg_match('/"'.$item.'"/i' , json_encode($array)); } and then I use it like: if(in_array_r($row['name'], $items_array)){ // something.. } It works unless the $row['name'] contains something like blah / blah / something , in which case it says that it can't find it in the array, even though it exists. How do I fix this? 回答1: This is because the slash in your input: blah / blah / something is seen as delimiter for the

Regular Expressions: low-caps, dots, zero spaces

我与影子孤独终老i 提交于 2019-12-20 03:54:15
问题 how do I write an expression which checks for lowcaps, dots, and without any white space in the string? the code below so far was trying to check for lowcaps and dots (it does not work anyway!) but I don't know how to add in the expression for white spaces. # check for matches of lowcaps or lowcaps with a dot if (!preg_match('/([a-z0-9]|[a-z0-9\.])/', $cst_value)) { $error = true; echo ' please use lowcaps only with dot(s) and without any spacing.'; } 回答1: [a-z0-9.] matches a lower-case