preg-match-all

How can I do a global regular expression match in Perl?

馋奶兔 提交于 2019-11-29 16:58:26
I am trying to come up with a regular expression in Perl matching multiple patterns and returning all of them like preg_match_all in PHP does. Here's what I have: $str = 'testdatastring'; if($str =~ /(test|data|string)/) { print "its found index location: $0 $-[0]-$+[0]\n"; print "its found index location: $1 $-[1]-$+[1]\n"; print "its found index location: $2 $-[2]-$+[2]\n"; print "its found index location: $3 $-[3]-$+[3]\n"; } This only gives me the first match which in this is 'test'. I want to be able to match all occurrences of specified patterns: 'test', 'data' and 'string'. I know that

Regex that extracts text between tags, but not the tags

﹥>﹥吖頭↗ 提交于 2019-11-29 14:55:38
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 You can use this following Regex: >([^<]*)< or, >[^<]*< Then eliminate unwanted characters like '<' & '>' the best way is to use Assertions, for your case, the regex would be: (?<=\<title\>).*?(?=\<\/title\>)

PHP preg_match_all limit

狂风中的少年 提交于 2019-11-29 11:45:49
I'm using preg_match_all for very long pattern. when run the code, i got this error : Warning: preg_match_all(): Compilation failed: regular expression is too large at offset 707830 After searching, I got the solution, so I should increase value of pcre.backtrack_limit and pcre.recursion_limit in php.ini But after I increase the value and restart my apache, it still got the same problem. My PHP verison is 5.3.8 increasing the PCRE backtrack and recursion limit may fix the problem, but will still fail when the size of your data hits the new limit. (doesn't scale well with more data) example: <

php preg_match_all numbers in parentheses

落花浮王杯 提交于 2019-11-28 14:29:13
I am trying to load a remote website and get all numbers that are inside of parentheses. But what ends up happening is it only matches the last value. Is my regex wrong? Am I using the correct flags? I have added the example of what it should match on in the second $html variable. //$html = file_get_contents("http://example.com/test.html"); $html = "(1234) (12) (1) \r\n (1346326)"; preg_match_all("^[(\d)]+$^", $html, $matches, PREG_PATTERN_ORDER); print_r($matches); echo "<br>"; foreach ($matches as $val) { echo "matched: " . $val[0] . "\n"; } Thanks. How about: preg_match_all("/\((\d+)\)/",

PHP preg_match_all limit

半城伤御伤魂 提交于 2019-11-28 05:43:52
问题 I'm using preg_match_all for very long pattern. when run the code, i got this error : Warning: preg_match_all(): Compilation failed: regular expression is too large at offset 707830 After searching, I got the solution, so I should increase value of pcre.backtrack_limit and pcre.recursion_limit in php.ini But after I increase the value and restart my apache, it still got the same problem. My PHP verison is 5.3.8 回答1: increasing the PCRE backtrack and recursion limit may fix the problem, but

Finding urls from text string via php and regex? [duplicate]

谁都会走 提交于 2019-11-28 04:44:18
问题 This question already has answers here : How to add anchor tag to a URL from text input (7 answers) Closed 5 years ago . I know the question title looks very repetitive. But some of the solution i did not find here. I need to find urls form text string: $pattern = '`.*?((http|https)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i'; if (preg_match_all($pattern,$url_string,$matches)) { print_r($matches[1]); } using this pattern i was able to find urls with http:// and https:// which is okey. But

PHP : simple regex problem

♀尐吖头ヾ 提交于 2019-11-28 02:26:54
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\}/ working okay for me, but it fails when i add new lines(\n) between {foreach}{/foreach} tags. You help

PHP/REGEX: Get a string within parentheses

前提是你 提交于 2019-11-28 00:10:00
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 replace the parentheses afterwards with str_replace or something, but that doesn't seem to be a very

php to extract a string from double quote

妖精的绣舞 提交于 2019-11-27 19:04:27
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. 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 a separate group. <?php $str = 'This is a text, "Your Balance left $0.10", End 0'; //forward slashes are the

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

徘徊边缘 提交于 2019-11-27 15:36:47
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, I found a function that uses parsing, which I rewrote to suit my needs. It can handle different kind