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);
Related
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
)
I am trying to get information out of a textarea that contains certain strings (e.g. [name]) and find each item encased in the square brackets using regex patterns (currently tried using preg_match, preg_split, preg_quote, preg_match_all). It seems that the problem is in my regex pattern that I am providing for it.
My current regex:
$menuItems = preg_match_all('/[^[][([^[].*)]/U', $_SESSION['emailBody'], $menuItems);
I have tried many other patterns e.g.
/(?[...]\w+): (?[...]\d+)/
Any help that can be provided with this is greatly appreciated.
EDIT:
Sample input:
[email] address [to] name [from] someone
Message displayed on var_dump of the $menuItems variable:
array(1) { [0]=> string(0) "" }
EDIT 2:
Thank you to everyone for the help and support with this, I am pleased to say that it is all up and running perfectly!
From the comment stream above, you can simplify the regular expression as follows:
preg_match_all('/\[(.*)\]/U', $_SESSION['emailBody'], $menuItems);
One thing to note:
preg_match_all() fills the array in its 3rd parameter with the results of the matches. Your example line then overwrites this array with the result of preg_match_all() (an integer).
You should then be able to iterate over the results by using the following loop:
foreach ($menuItems[1] as $menuItem) {
// ...
}
Escape the square brackets and remove the dot:
$menuItems = preg_match_all('/[^[]\[([^[]*)\]/U', $_SESSION['emailBody'], $menuItems);
// here __^ __^ ^
preg_match_all doesn't return a string. You have to add an array for the last parameter:
preg_match_all('/\[([^[\]]*)\]/U', $_SESSION['emailBody'], $matches);
The matches are in the array $matches
print_r($matches);
Working example:
$str = '[email] address [to] name [from] someone';
preg_match_all('/\[([^[\]]*)\]/U', $str, $matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => [email]
[1] => [to]
[2] => [from]
)
[1] => Array
(
[0] => email
[1] => to
[2] => from
)
)
Here is a simple solution. This regex will capture all items encased in brackets along with brackets as well.
If you don't want brackets in result change regex to $regex = "/(?:\\[(\\w+)\\])/mi";
$subject = "[email] address [to] name [from] someone";
$regex = "/(\\[\\w+\\])/mi";
$matches = array();
preg_match_all($regex, $subject, &$matches);
print_r($matches);
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
I have the following text string:
-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL
What regex code do I need to extract the values so that they appear in the matches array like this:
-asc100-17
-asc100-17A
-asc100-17BPH
-asc100-17ASL
Thanks in advance!
You may try this:
$str = "-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL";
preg_match_all('/-asc\d+-[0-9a-zA-Z]+/', $str, $matches);
// Print Result
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => -asc100-17
[1] => -asc100-17A
[2] => -asc100-17BPH
[3] => -asc100-17ASL
)
)
Based on the very limited information in your question, this works:
-asc100-17[A-Z]*
Debuggex Demo
If you want to capture the post -asc100- code, then use
-asc100-(17[A-Z]*)
Which places 17[the letters] into capture group one.
Might use preg_split with a lookahead as well for your scenario:
print_r(preg_split('/(?=-asc)/', $str, -1, PREG_SPLIT_NO_EMPTY));
Are you trying to break the string in an array? Then why regex is required? This function can handle what you want:
$arr = explode('-asc', '-asc100-17-asc100-17A-asc100-17BPH-asc100-17ASL');
foreach ($arr as $value) {
if(!empty($value)){
$final[] = '-asc'.$value;
}
}
print_r($final);
Output array : Array ( [0] => -asc100-17 [1] => -asc100-17A [2] => -asc100-17BPH [3] => -asc100-17ASL )
I have a string contains the following pattern "[link:activate/$id/$test_code]" I need to get the word activate, $id and $test_code out of this when the pattern [link.....] occurs.
I also tried getting the inside items by using grouping but only gets active and $test_code couldn't get $id. Please help me to get all the parameter and action name in array.
Below is my code and output
Code
function match_test()
{
$string = "Sample string contains [link:activate/\$id/\$test_code] again [link:anotheraction/\$key/\$second_param]]] also how the other ationc like [link:action] works";
$pattern = '/\[link:([a-z\_]+)(\/\$[a-z\_]+)+\]/i';
preg_match_all($pattern,$string,$matches);
print_r($matches);
}
Output
Array
(
[0] => Array
(
[0] => [link:activate/$id/$test_code]
[1] => [link:anotheraction/$key/$second_param]
)
[1] => Array
(
[0] => activate
[1] => anotheraction
)
[2] => Array
(
[0] => /$test_code
[1] => /$second_param
)
)
Try this:
$subject = <<<'LOD'
Sample string contains [link:activate/$id/$test_code] again [link:anotheraction/$key/$second_param]]] also how the other ationc like [link:action] works
LOD;
$pattern = '~\[link:([a-z_]+)((?:/\$[a-z_]+)*)]~i';
preg_match_all($pattern, $subject, $matches);
print_r($matches);
if you need to have \$id and \$test_code separated you can use this instead:
$pattern = '~\[link:([a-z_]+)(/\$[a-z_]+)?(/\$[a-z_]+)?]~i';
Is this what you are looking for?
/\[link:([\w\d]+)\/(\$[\w\d]+)\/(\$[\w\d]+)\]/
Edit:
Also the problem with your expression is this part:
(\/\$[a-z\_]+)+
Although you have repeated the group, the match will only return one because it is still only one group declaration. The regex won't invent matching group numbers for you (Not that i've ever seen anyway).