I want to get the contents of parenthesis within a string in PHP by regular expression. I tried this regex
preg_match_all('/\((.*)?\)/', $string, $match);
But this get the content between the first ( and last ). How can I get the content of every ( )separately to make the array of match?
You need to change the .* with [^\)]*
preg_match_all('/\(([^)]*)\)/', $string, $match);
Related
I'm trying to write a regex to extract the value of a particular parameter from an array of content in a modx database. the format of the tags is:
[[!video? &path=`_path_to_video` &width=`123` &height=`123` &someotherparm=`bar`]]
I am trying to get the content of the &path parameter using this regex:
preg_match_all('/\[\[path=`(.*?)`\]\]/', $content, $tags, PREG_PATTERN_ORDER) ;
but without luck - it just returns empty arrays when I dump the $tags variable.
something wrong with my regex?
Your pattern doesn't match the tag format:
preg_match_all('/&path=`([^`]+)`/', $content, $tags, PREG_PATTERN_ORDER);
Match &path= then backtick, then
Match anything that is not a backtick up until another backtick and capture it
If you really need to match the existence of [[ and closing ]] then:
preg_match_all('/\[\[.*&path=`([^`]+)`.*\]\]/', $content, $tags, PREG_PATTERN_ORDER);
Try this regex:
'/\[\[.*?&path=`([^`]+?)`.*?\]\]/'
I'm a regex-noobie, so sorry for this "simple" question:
I've got an URL like following:
http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx
what I'm going to archieve is getting the number-sequence (aka Job-ID) right before the ".aspx" with preg_replace.
I've already figured out that the regex for finding it could be
(?!.*-).*(?=\.)
Now preg_replace needs the opposite of that regular expression. How can I archieve that? Also worth mentioning:
The URL can have multiple numbers in it. I only need the sequence right before ".aspx". Also, there could be some php attributes behind the ".aspx" like "&mobile=true"
Thank you for your answers!
You can use:
$re = '/[^-.]+(?=\.aspx)/i';
preg_match($re, $input, $matches);
//=> 146370543
This will match text not a hyphen and not a dot and that is followed by .aspx using a lookahead (?=\.aspx).
RegEx Demo
You can just use preg_match (you don't need preg_replace, as you don't want to change the original string) and capture the number before the .aspx, which is always at the end, so the simplest way, I could think of is:
<?php
$string = "http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-146370543.aspx";
$regex = '/([0-9]+)\.aspx$/';
preg_match($regex, $string, $results);
print $results[1];
?>
A short explanation:
$result contains an array of results; as the whole string, that is searched for is the complete regex, the first element contains this match, so it would be 146370543.aspx in this example. The second element contains the group captured by using the parentheeses around [0-9]+.
You can get the opposite by using this regex:
(\D*)\d+(.*)
Working demo
MATCH 1
1. [0-100] `http://stellenanzeige.monster.de/COST-ENGINEER-AUTOMOTIVE-m-w-Job-Mainz-Rheinland-Pfalz-Deutschland-`
2. [109-114] `.aspx`
Even if you just want the number for that url you can use this regex:
(\d+)
I need the help of a regex wizard, or someone who knows more about this than me (which means there's lots of candidates :)
I am trying to match everything that occurs between the first and second slash, excluding those slashes, or nothing if there's no starting and trailing slash:
$subject = '/1234-abcd/blahblah';
$pattern = '/^\/(.*)\//';
preg_match($pattern, $subject, $matches);
print_r($matches);
Here are the results:
Array
(
[0] => /1234-abcd/
[1] => 1234-abcd
)
I'm close. $matches[1] has the result I'm after, but it's not matching this as its first array item (and instead, the first captured subpattern).
How do I exclude the starting and trailing slashes in this regex pattern?
Thanks!
You can use this regex:
$pattern = '#(?<=/)[^/]+#';
And use preg_match_all instead of preg_match
PS: Note that you can also use explode to split your input by / and avoid using regex altogether.
I'm trying to build out a preg_match_all so I can use the array, but the problem is that I can't refine my regex to work.
An example input would be:
#|First Name|#
This text shouldn't be caught
#|Second Value|#
First Name and Second Value should both be in the array.
Here's what I've tried:
preg_match_all('/\#\|(.*?)\|\#\]/',$html, $out);
and
preg_match_all ("/^#\|*\|#*$/i", $html, $out);
The first is about right, it only contains \] near the end which breaks what you want. Using
/\#\|(.*?)\|\#/
does give me correct results, with the matches in $out[1].
Try this regexp :
/\#\|(.*?)\|\#/
Try with:
preg_match_all('/#\|(.*?)\|#/', $html, $out);
Escaping # should not be needed. To specify no # between #'s, you might also use
preg_match_all('/#\|([^#]*)\|#/', $html, $out);
a couple of things, you need to add the '.' character in between your bars. This character means match any character, without it you are saying match zero or more '|' characters. you also need to add the m modifier for multiline matching.
/^#\|.*\|#$/im
Is it possible to have a PHP regex expression that extracts the content from the first ] to the last [?
For example if I had the following string:
$string = [shortcode]You write a shortcode by using ([])[/shortcode]
I would want to extract:
You write a shortcode by using brackets ([])
and store it in a variable. The content to be extracted could be anything. Thanks in advance.
You should be using capturing groups to make sure you match the closing tag.
\[(\w+)\].*?\[/\1\]
This will match a word inside [] and keep going until if finds the same word inside [/...].
Regexes are greedy by default, so this will do the job just fine:
/\](.*)\[/
To get this working in PHP properly, you would do something like this:
preg_match('/\](.*)\[/', $text, $matches);
$result = $matches[1];
this could make, what you need
[^\]]\](.*)\[[^\[]
This works:
preg_match( '#\](.*)\[#', $string, $matches);
print_r($matches);