Reputation: 6013
I am trying to capture the following pattern:
<a href="http://cdn.xyz.com/media/info.pdf" target="_blank">
This is what I am trying:
preg_match_all( '/(<[a-zA-Z]+[^>]+>)/ism', $str, $matches);
This is not capturing the above pattern.
How should I restructure the pattern?
Upvotes: 0
Views: 43
Reputation: 174854
You could to use a negative lookahead assertion based regex.
preg_match_all('~<[a-zA-Z]+(?:(?!&[lg]t;).)*>~isg', $str, $matches);
(?:(?!&[lg]t;).)*
matches any character but not of <
or >
. That is, it checks whether the character going to be matched won't be the starting letter in <
or >
.
OR
<[a-zA-Z]+.*?>
Upvotes: 1
Reputation: 67988
(<[a-zA-Z]+.+?>)
You can use this simple pattern.See demo.
https://regex101.com/r/bW3aR1/14
Your regex contains [^>]+
which is not what you think.It is just a class of characters which should not appear.The order is not defined over here.
Upvotes: 1