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
  • 643,351
  • 109
  • 780
  • 844
Grego
  • 2,220
  • 9
  • 43
  • 64
  • 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
  • 91,536
  • 11
  • 82
  • 95
1

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

 preg_match('/<a href=.*?>([^<>]+)/', $string, $res);
mario
  • 144,265
  • 20
  • 237
  • 291