Skip to content

Instantly share code, notes, and snippets.

@keitheis
Created May 20, 2017 07:24
Show Gist options
  • Save keitheis/dbbe6b520ce028cda005b0d98b615b08 to your computer and use it in GitHub Desktop.
Save keitheis/dbbe6b520ce028cda005b0d98b615b08 to your computer and use it in GitHub Desktop.
import re
# module global
room_terms_match = {
'rooms': r'房',
'livingrooms': r'廳',
}
re_room_compiled_patterns = {
term: re.compile(r'(\d+){}'.format(value))
for term, value in room_terms_match.items()
}
def fetch_room_terms_amound(string_to_match):
found = {}
for key, pattern in re_room_compiled_patterns.items():
matched = pattern.search(string_to_match)
if matched:
found[key] = matched.group(1)
return found
def main():
string = "5房2廳4衛1陽台"
found = fetch_room_terms_amound(string)
import alog
alog.info(alog.pformat(found))
if __name__ == "__main__":
main()
# 2017-05-20 15:23:40 INFO [fetch_room_amound:30]
# {'livingrooms': '2', 'rooms': '5'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment