0

How do I negate a set of characters together in Regular Expression? Ex: [^</a>] (When you find stop matching) when I do this way the code stops when it finds an "a" in the text

How do I do to negate a set of characters

This is the expression:

$string = "<a href='asdasd'>lalalala</a>";
preg_match('/<a href=.*?>([^<\/a>]+)/',$string,$res);
Gumbo
  • 620,600
  • 104
  • 758
  • 828
Grego
  • 2,178
  • 9
  • 39
  • 61
  • 2
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Dec 01 '11 at 08:39

2 Answers2

2

You can use negative lookahead:

.(?!</a>)

will match any character not followed by </a>.

Thus, to match a whole string that doesn't contain </a>:

^(.(?!</a>))*$
Petar Ivanov
  • 88,488
  • 10
  • 77
  • 93
1

As simpler alternative to an assertion one could just match text content, stop at tag delimiters:

 preg_match('/<a href=.*?>([^<>]+)/', $string, $res);
mario
  • 141,508
  • 20
  • 234
  • 284