Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdalisue/f2c9ab121883e48d3c2f to your computer and use it in GitHub Desktop.
Save lambdalisue/f2c9ab121883e48d3c2f to your computer and use it in GitHub Desktop.
Python check if the partial part of text is quoated
# coding=utf-8
"""
"""
__author__ = 'Alisue <lambdalisue@hashnote.net>'
import re
pattern = re.compile("[^'\"]")
def is_quoated(text, s, e):
p = pattern.sub('', text[0:s])
n = pattern.sub('', text[e:-1])
return len(p) > 0 and len(n) > 0 and (len(p) * len(n)) % 2 != 0
def __unittest__():
# 0123456789012345678901234
text = """N'Y'N'Y'N"Y"N"Y'Y'Y"N"Y"N"""
# N
assert is_quoated(text, 0, 1) is False
# N'
assert is_quoated(text, 0, 2) is False
# N'Y
assert is_quoated(text, 0, 3) is False
# N'Y'
assert is_quoated(text, 0, 4) is False
# '
assert is_quoated(text, 1, 2) is False
# 'Y
assert is_quoated(text, 1, 3) is False
# 'Y'
assert is_quoated(text, 1, 4) is False
# 'Y'N
assert is_quoated(text, 1, 5) is False
# Y
assert is_quoated(text, 2, 3) is True
# Y'
assert is_quoated(text, 2, 4) is False
# Y'N
assert is_quoated(text, 2, 5) is False
# Y'N'
assert is_quoated(text, 2, 6) is True
# Y'N'Y
assert is_quoated(text, 2, 7) is True
# Y'N'Y'
assert is_quoated(text, 2, 8) is False
if __name__ == '__main__':
__unittest__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment