-1

I want to use the python re package to search for strings that start with "[[" that is not followed by "Category:" has an arbitrary number of characters and end with "]]". I tried the following code:

s="blah [[Category:Cartooning]] blah"
regex = re.compile(r"\[\[(?<!Category:).*?\]\]")
res = regex.search(s)
if res!=None:
    print(res)
else:
    print('no match')

and got the following response:

<re.Match object; span=(5, 28), match='[[Category:Cartooning]]'>

Seems like negative lookbehind does not work. What am I doing wrong? Thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
KostasD
  • 97
  • 1
  • 4

1 Answers1

0

You can check using negative lookahead instead. Also check using is not None. Passing and failing text added as well:

import re

regex = re.compile(r"\[\[(?!Category:).*?\]\]")

s = "blah [[Category:Cartooning]] blah"
keep = "blah [[Cat:Cartooning]] blah"
texts = [s, keep]

results = [regex.search(i) for i in texts]
for res in results:
    if res is not None:
        print(res)
    else:
        print('no match')

Returns:

no match
<re.Match object; span=(5, 23), match='[[Cat:Cartooning]]'>
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53