preg-match-all

Regex that extracts text between tags, but not the tags

拜拜、爱过 提交于 2019-11-27 08:21:57
问题 I want to write a regex which extract the content that is between two tags <title> in a string but not the tags. IE I have the following <title>My work</title> <p>This is my work.</p> <p>Learning regex.</p> The regex (<title>)(.*?)(<\/title>) extracts <title>My work</title> but I want to extract only My work . How can I do that? This is a link to the example http://regex101.com/r/mD8fB0 回答1: You can use this following Regex: >([^<]*)< or, >[^<]*< Then eliminate unwanted characters like '<' &

PHP preg_match and preg_match_all functions

六眼飞鱼酱① 提交于 2019-11-27 07:33:55
I would like to know what the preg_match and preg_match_all functions do and how to use them. preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match. http://php.net/manual/en/function.preg-match-all.php Sumit Both preg_match and preg_match_all functions in PHP use Perl compatible regular expressions. You can watch this series to fully understand Perl compatible regular expressions: https://www.youtube.com/watch?v

PHP preg_match and preg_match_all functions

痞子三分冷 提交于 2019-11-27 03:59:11
问题 I would like to know what the preg_match and preg_match_all functions do and how to use them. 回答1: preg_match stops looking after the first match. preg_match_all , on the other hand, continues to look until it finishes processing the entire string. Once match is found, it uses the remainder of the string to try and apply another match. http://php.net/manual/en/function.preg-match-all.php 回答2: Both preg_match and preg_match_all functions in PHP use Perl compatible regular expressions. You can

Get repeated matches with preg_match_all()

会有一股神秘感。 提交于 2019-11-27 02:14:29
I'm trying to get all substrings matched with a multiplier: $list = '1,2,3,4'; preg_match_all('|\d+(,\d+)*|', $list, $matches); print_r($matches); This example returns, as expected, the last match in [1] : Array ( [0] => Array ( [0] => 1,2,3,4 ) [1] => Array ( [0] => ,4 ) ) However, I would like to get all strings matched by (,\d+) , to get something like: Array ( [0] => ,2 [1] => ,3 [2] => ,4 ) Is there a way to do this with a single function such as preg_match_all() ? Benjamin According to Kobi (see comments above): PHP has no support for captures of the same group Therefore this question

How to get all captures of subgroup matches with preg_match_all()? [duplicate]

空扰寡人 提交于 2019-11-27 01:49:50
This question already has an answer here: Get repeated matches with preg_match_all() 6 answers Update/Note: I think what I'm probably looking for is to get the captures of a group in PHP. Referenced: PCRE regular expressions using named pattern subroutines. (Read carefully:) I have a string that contains a variable number of segments (simplified): $subject = 'AA BB DD '; // could be 'AA BB DD CC EE ' as well I would like now to match the segments and return them via the matches array: $pattern = '/^(([a-z]+) )+$/i'; $result = preg_match_all($pattern, $subject, $matches); This will only return

PHP : simple regex problem

我们两清 提交于 2019-11-26 23:43:03
问题 Some of my HTML files contains string like : {foreach $any_kind_of_charaters} Any kind of string including "\n\r" and spaces here {/foreach} I want to apply PHP's preg_match_all on them and wanna return a nice array like printed below Array ( [0] => {foreach $any_kind_of_charaters} Any kind of string including "\n\r" and spaces here {/foreach} [1] => any_kind_of_charaters [2] => Any kind of string including "\n\r" and spaces here ) This REGEX : /\{foreach\s+\$(.*)\}\s+(.*)\s+\{\/foreach\}/

php to extract a string from double quote

夙愿已清 提交于 2019-11-26 22:45:36
问题 I have a string: This is a text, "Your Balance left $0.10", End 0 How can I extract the string in between the double quotes and have only the text (without the double quotes): Your Balance left $0.10 I have tried preg_match_all() but with no luck. 回答1: As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern Double-quote At least one non-double-quote Double-quote The brackets around the [^"]+ means that that portion will be returned as

PHP: unexpected PREG_BACKTRACK_LIMIT_ERROR

旧巷老猫 提交于 2019-11-26 21:54:02
问题 function recursiveSplit($string, $layer) { $err = preg_match_all("/\{(([^{}]*|(?R))*)\}/",$string,$matches); echo "Elementi trovati: $err<br>"; if($err == FALSE) echo "preg_match_all ERROR<br>"; // iterate thru matches and continue recursive split if (count($matches) > 1) { for ($i = 0; $i < count($matches[1]); $i++) { if (is_string($matches[1][$i])) { if (strlen($matches[1][$i]) > 0) { echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />"; recursiveSplit($matches[1][$i], $layer + 1);

PHP/REGEX: Get a string within parentheses

浪尽此生 提交于 2019-11-26 21:39:12
问题 This is a really simple problem, but I couldn't find a solution anywhere. I'm try to use preg_match or preg_match_all to obtain a string from within parentheses, but without the parentheses . So far, my expression looks like this: \([A-Za-z0-9 ]+\) and returns the following result: 3(hollow highlight) 928-129 (<- original string) (hollow highlight) (<- result) What i want is the string within parentheses, but without the parentheses. It would look like this: hollow highlight I could probably

PHP: split string on comma, but NOT when between braces or quotes?

眉间皱痕 提交于 2019-11-26 17:14:17
问题 In PHP I have the following string : $str = "AAA, BBB, (CCC,DDD), 'EEE', 'FFF,GGG', ('HHH','III'), (('JJJ','KKK'), LLL, (MMM,NNN)) , OOO"; I need to split this string into the following parts: AAA BBB (CCC,DDD) 'EEE' 'FFF,GGG' ('HHH','III') (('JJJ','KKK'),LLL, (MMM,NNN)) OOO I tried several regexes, but couldn't find a solution. Any ideas? UPDATE I've decided using regex is not really the best solution, when dealing with malformed data, escaped quotes, etc. Thanks to suggestions made on here,