0

I want to check string which is not preceded by [a-zA-Z] before attachment.

def is_attachment_url(url):
    """check url"""
    pattern = '(?<![\w]+)attachment'
    return re.search(pattern, url, re.I)


tests = (
    'article_?attachment',  # should be false
    'article_fattachment',  # should be false
    'article_-attachment',  # should be true
    'article_/attachment',  # should be true
)
for ss in tests:
    print(is_attachment_url(ss))

error tips:

    raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
Mario7
  • 83
  • 1
  • 12
  • Why would `article_?attachment` be false, but `article_-attachment` be true? The word "attachment" is preceded by a non-word character in both examples – Addison Jul 27 '17 at 04:29
  • 1
    Possible duplicate of [Python Regex Engine - "look-behind requires fixed-width pattern" Error](https://stackoverflow.com/questions/20089922/python-regex-engine-look-behind-requires-fixed-width-pattern-error) – Addison Jul 27 '17 at 04:32
  • When `attachment` after `?`,it may means not a attachment,and when `attachment` after `-` may be a `attachment url`,so i `attachment` should not after `[a-z]` or `-` or `/` – Mario7 Jul 27 '17 at 04:38

1 Answers1

0

The + in the pattern makes it variable-width. You don't need it, since you only want to check the single character before "attachment", so just remove it:

pattern = '(?<![\w])attachment'

Tara McGrew
  • 1,987
  • 19
  • 28