Jump to content


negating entire string vs. class

  • Please log in to reply
No replies to this topic

#1 akitchin

  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 21 July 2006 - 04:48 AM

i'm trying to match an HTML tag that has no ending tag of the same type.  my question is, how do i make it match any character/string that is NOT </tagname>?  i'm currently using the character class [^</tagname>], but i'm thinking these days that it will match anything that isn't a <, a letter in the tagname, or a >.  am i wrong?  if so, how to i make sure it will match "anything that isn't </tagname>"?

current regex:

$pattern = '@<'.$tagname.'[^<>]*?>[^</'.$tagname.'>]*?$@si'

in addition, i guess my use of the ungreedy-modifying '?' in the second portion there is useless, since it will only match if it runs up to the end of the string (thus no amount of (un)greediness will change the matching nature)?

EDIT:  solved it myself.  needed to use a lookahead pattern, to make sure that it was matching the tag, any number of any character, that was NOT followed by the closing tag.  current pattern:

$pattern = '@<'.$tagname.'[^<>]*?>.*?(?!</'.$tagname.'>)$@si';

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users