K
K
khodos_dmitry2018-05-02 19:54:55
Regular Expressions
khodos_dmitry, 2018-05-02 19:54:55

Why doesn't assertion work in regular expression?

#(<img.+>\s*)(?!</a>)#Uu
In this regular expression, you need to select the img tag, after which the tag does not follow </a>. It finds all img tags, that is, it does not work correctly.
If you look for the img tag after which there is </a>, then such a similar regular expression works:
#(<img.+>\s*)(?=</a>)#Uu

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-05-02
@khodos_dmitry

#(<img.+>\s*)(?!</a>)#Uu
In this regular expression, you need to select the img tag, after which the tag does not follow </a>. It finds all img tags, that is, it does not work correctly.

A slightly incorrect "statement", more precisely, your statement (looking) ahead only works for \s*and you have it with a quantifier *- zero or many , and it turns out that in this case, "zero" repetitions of the "previous" ( \s) work and therefore, as it were, looks forward
if you want to pull out all the imgtags after which there are none </a>, then you should write something like this,
and if you are worried that there may be whitespace characters, then something like this
https://regex101.com/r/IoAn7q/ one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question