Don't use a regular expression. Use a HTML parser. BeautifulSoup makes the task easy:
from bs4 import BeautifulSoup
soup = BeautifulSoup(htmltext)
text = soup.find('textarea', id='lao').string
If you then need to limit the result to just Lao characters, you can further process the text
variable.
However, the Python re
module isn't that strong (yet) when it comes to Unicode. Your options are to use a regular expression to just grab code points in the range 0E80–0EFF, use the unicodedata
module and filter on the unicode codepoint name, or use the regex
library to only match Lao characters.
Using a regular expression:
import re lao_codepoints = re.compile(ur'[\u0e80-\u0eff]', re.UNICODE) lao_text = u''.join(lao_codepoints.findall(text))
Demo:
>>> print u''.join(lao_codepoints.findall(text)) ຮັກມັກຫຼາຍຢາກໄດ້ຫຼາຍຢາກເຮັດຫຼາຍປະເພດຄໍາກໍາມະການອອກສຽງເລັຟຄວາມຮັກຄົນຮັກຄູ່ຮັກສິ່ງທີ່ເຈົ້າຮັກທີ່ຮັກເທັນນິດສູນຮັກຜູ້ໃດຜູ້ໜຶ່ງຮ່ວມປະເວນີປະເພດຄຳນາມການອອກສຽງເລັຟ
Using the
unicodedata
module:import unicodedata loa_text = u''.join([ch for ch in text if unicodedata.name(ch, '').startswith('LAO')])
Demo:
>>> print u''.join([ch for ch in text if unicodedata.name(ch, '').startswith('LAO')]) ຮັກມັກຫຼາຍຢາກໄດ້ຫຼາຍຢາກເຮັດຫຼາຍປະເພດຄໍາກໍາມະການອອກສຽງເລັຟຄວາມຮັກຄົນຮັກຄູ່ຮັກສິ່ງທີ່ເຈົ້າຮັກທີ່ຮັກເທັນນິດສູນຮັກຜູ້ໃດຜູ້ໜຶ່ງຮ່ວມປະເວນີປະເພດຄຳນາມການອອກສຽງເລັຟ
Using the
regex
module:import regex lao_codepoints = regex.compile(ur'\p{Lao}', regex.UNICODE) lao_text = u''.join(lao_codepoints.findall(text))
Demo:
>>> print u''.join(lao_codepoints.findall(text)) ຮັກມັກຫຼາຍຢາກໄດ້ຫຼາຍຢາກເຮັດຫຼາຍປະເພດຄໍາກໍາມະການອອກສຽງເລັຟຄວາມຮັກຄົນຮັກຄູ່ຮັກສິ່ງທີ່ເຈົ້າຮັກທີ່ຮັກເທັນນິດສູນຮັກຜູ້ໃດຜູ້ໜຶ່ງຮ່ວມປະເວນີປະເພດຄຳນາມການອອກສຽງເລັຟ