newbie: php preg_match_all not working as expected [duplicate] - php

This question already has answers here:
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 8 years ago.
i'm trying to parse tags from a string like the following:
$string = "foo [cmd:tag1] bar [cmd:tag2] bla bla";
$pattern = "/\[cmd:(.+)\]/";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
$rc = $matches[0];
foreach($rc as $tag)
{
print_r2($tag);
}
which will return:
Array
(
[0] => [cmd:tag1] bar [cmd:tag2]
[1] => 4
)
what is wrong in my syntax as i'm expecting the following result:
Array
(
[0] => [cmd:tag1]
[1] => [cmd:tag2]
)
thanks

\[cmd:(.+?)\]
or use
\[cmd:([^\]]*)\]
Make your quantifier * non greedy by putting ? ahead of it.
See demo.
https://regex101.com/r/fA6wE2/23

Related

pregreplace retunrs empty string [duplicate]

This question already has answers here:
Is there a PHP function that can escape regex patterns before they are applied?
(2 answers)
RegEx for word boundary but still match if is preceded or followed by special chars
(1 answer)
Closed 4 years ago.
I am trying to highlight words in a text by using pregreplace. The words are within an array which I do feed into pregreplace. This used to work, but stopped maybe due to upgrading php in the past, now it returns nothing.
// Function highlights $words in $str
function highlight_words($str, $words) {
global $color;
if(is_array($words)) {
foreach($words as $k => $word) {
// $pattern[$k] = "~\b($word)\b~is";
$pattern[$k] = "/$word/";
$replace[$k] = '<span style="background: '.$color[2].';color:'.$color[4].';">\\1</span>';
}
}
else {
$pattern = "~\b($words)\b~is";
$replace = '<span style="background: '.$color[2].';color:'.$color[4].';">\\1</span>';
}
return preg_replace($pattern,$replace ,$str);
}
echo highlight_words($text, $words);
values of pattern:
Array
(
[0] => /$18.5mUSD/
[1] => /$8,000,000.00/
[2] => /(at)/
[3] => /+43 688 649 45702/
...
values of $replace:
Array
(
[0] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[1] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[2] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
[3] => <span style="background: #FF6B02;color:#FFFFFF;">\1</span>
...
preg_replace simply returns nothing. Seems to be a problem with array, but the syntax looks ok. If I change preg_replace to replace some simply regex pattern it will do, but it does not work with the array.
How can this be fixed?

Geting two matches in a string using a regular expression [duplicate]

This question already has answers here:
PHP Regular Expression: string from inside brackets
(3 answers)
Closed 5 years ago.
My string:
fields[name_1]
I want to get fields and name_1 using regex.
I'm know about preg_match_all(), but I'm not friends with regular expressions.
This can be used for direct match:
$string = 'fields[name_1]';
preg_match('/(.+)\[(.+)\]/', $string, $matches);
print_r($matches);
You get:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)
So, $matches[1] and $matches[2] are what you needed.
Still I am unclear about your exact need!
Here are the explanation for the Regex:
https://regex101.com/r/PcJzQL/3
http://www.phpliveregex.com/
https://www.functions-online.com/preg_match.html
THere are uncounted examples for this alone here on SO. A simple search would have shown you what you need. Anyway, to get you going:
<?php
$subject = 'fields[name_1]';
preg_match('/^(.+)\[(.+)]$/', $subject, $tokens);
print_r($tokens);
The output of that obviously is:
Array
(
[0] => fields[name_1]
[1] => fields
[2] => name_1
)

Php preg_match_all does not behave as expected [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
Trying to capture all text between tags.
Code:
$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';
$match = '/<test>(.*)<tests>/';
preg_match_all($match, $test, $nextLink);
Result of print_r:
Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) )
your regex syntax is greedy. use folowing:
$match = '/<test>(.*?)<tests>/';

How to extarct the string words and special characters between curly brackets in php preg_match [duplicate]

This question already has answers here:
Regex extract string between 2 curly braces
(5 answers)
Closed 8 years ago.
I have String like this
$string ="this is test {username} ,{password#123} and Other {asdfg##$}"
I want this string as array format as follows
[1] => Array
(
[0] => username
[1] => password#123
[2] => asdfg##$
)
$re = "/\{(.*?)\}/";
$str = "this is test {username} ,{password#123} and Other {asdfg##\$}";
preg_match_all($re, $str, $matches);
print_r($matches);
this gives what you want
(?<={)[^}]+(?=})
Try this.This should work.
$re = "/(?<={)[^}]+(?=})/mi";
$str = "this is test {username} ,{password#123} and Other {asdfg##\$}";
preg_match_all($re, $str, $matches);

php newbie problem: regex [duplicate]

This question already has answers here:
Matches text inside brackets with Regex in PHP
(5 answers)
Closed 10 years ago.
i'm sorry i've already asked for this but couldn't find a solution yet :(
here's my string: (as you can see it has linebreaks)
Webname: [webname]
Username: [username]
IP: [IP]
i need to read out the values inside the square brackets.
here's my code:
$pattern = '/\[(.|\n)+?\]/'; // i've used the same syntax for my asp projects, always worked
preg_match($pattern, $txt, $matches, PREG_OFFSET_CAPTURE);
echo "matches:".count($matches)."\n\n";
foreach ($matches as $match)
{
echo $match[0]."\n";
}
i'm getting only 2 matches: [webname] and e (???)
i'm fiddling with this for hours now and can't find out what's wrong ..
any ideas?
thanks
Looks more complicated than it has to be. Line breaks don't play any role here.
$pattern = '/\[(.+?)\]/';
preg_match_all($pattern, $txt, $matches);
print_r($matches);
gives
Array
(
[0] => Array
(
[0] => [webname]
[1] => [username]
[2] => [IP]
)
[1] => Array
(
[0] => webname
[1] => username
[2] => IP
)
)
So the values would be in $matches[1]. If you want the values including the brackets ($matches[0]), you can also omit the parenthesis in the pattern.
The first capture group, in your case there is only one, is in $matches[1]
Try something like
$pattern = '/\[([^\]]+)\]/';
and use preg_match_all() to get all matches.
Try this
$values = array();
foreach (explode("\n", $text) as $line) {
if (preg_match('/([^:]++):[^\s]*+(.*+)/', $line, $match)) {
$values[$match[1]] = $match[2];
}
}
var_dump($values);

Categories