forked from ajing2/coupon-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
57 lines (35 loc) · 1.18 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
from utils import getMatchString
class SearchingKeyRegex:
_instance = None
def __init__(self):
self.rules = list()
with open('templates/search.json') as fp:
obj = json.loads(fp.read())
if obj is None:
return None
for rule in obj['rule-list']:
self.rules.append(r'{}'.format(rule['rule']))
def parseImpl(self, content):
for i in range(1):
start = content.find('#')
if start < 0:
break
end = content.rfind('#')
if end is start:
break
return content[start:end+1]
for rule in self.rules:
value = getMatchString(content, rule)
if value is not None:
value = value.strip()
if len(value) > 0:
return '#{}#'.format(value)
return None
@staticmethod
def parse(content):
if SearchingKeyRegex._instance is None:
SearchingKeyRegex._instance = SearchingKeyRegex()
return SearchingKeyRegex._instance.parseImpl(content)