python - Regular expression with Lao? -


in python display lao characters in html code (just in "textarea" tag):

<font color="red">ພິມຄໍາສັບລາວ ຫຼື ອັງກິດແລ້ວກົດປຸ່ມຄົ້ນຫາ - enter english or lao hit search</font><br /> <center><table id='display' border='0' width='100%'>   <tr>     <td id='lao2' colspan='3' style='height: 18px; text-align: left'>       <span style='color: #660033'><span style='font-size: 12pt'>&nbsp;&nbsp;&nbsp;</span></span>&nbsp;&nbsp;     </td>   </tr>   <tr>     <td style='width: 120px'>&nbsp;</td>     <td style='width: 192px'>       <textarea id='lao' font-name='phetsarath ot' font-size='12' rows='10' cols='84' readonly='readonly'>     1.  (loved, loving)       1. ຮັກ       2. ມັກຫຼາຍ       3. love ຢາກໄດ້ຫຼາຍ, ຢາກເຮັດຫຼາຍ       ປະເພດ: ຄໍາກໍາມະ       ການອອກສຽງ: ເລັຟ      2.       1. ຄວາມຮັກ       2. ຄົນຮັກ, ຄູ່ຮັກ, ສິ່ງທີ່ເຈົ້າຮັກ       3. ທີ່ຮັກ, (ເທັນນິດ) ສູນ       in love ຮັກຜູ້ໃດຜູ້ໜຶ່ງ       make love ຮ່ວມປະເວນີ       ປະເພດ: ຄຳນາມ       ການອອກສຽງ: ເລັຟ       </textarea>     </td>     <td style='width: 284px'>&nbsp;&nbsp;</td>   </tr>   <tr>     <td>&nbsp;</td>     <td>&nbsp;</td>     <td>&nbsp;</td>   </tr>   <tr>     <td>&nbsp;</td>     <td id='lao1' align='center'>ກະຊວງ ໄປສະນີ, ໂທລະຄົມມະນາຄົມ ແລະ ການສື່ສານ</td><td>&nbsp;</td>   </tr>   <tr>     <td>&nbsp;</td>     <td id='lao1' align='center'>ສູນບໍລິຫາລັດດ້ວຍເອເລັກໂຕຣນິກ</td><td>&nbsp;</td>   </tr> </table></center><br /> 

i want value in "textarea". should do?

don't use regular expression. use html parser. beautifulsoup makes task easy:

from bs4 import beautifulsoup  soup = beautifulsoup(htmltext) text = soup.find('textarea', id='lao').string 

if need limit result just lao characters, can further process text variable.

however, python re module isn't strong (yet) when comes unicode. options use regular expression grab code points in range 0e80–0eff, use unicodedata module , filter on unicode codepoint name, or use regex library match lao characters.

  • using 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 unicodedata module:

    import unicodedata  loa_text = u''.join([ch ch in text if unicodedata.name(ch, '').startswith('lao')]) 

    demo:

    >>> print u''.join([ch ch in text if unicodedata.name(ch, '').startswith('lao')]) ຮັກມັກຫຼາຍຢາກໄດ້ຫຼາຍຢາກເຮັດຫຼາຍປະເພດຄໍາກໍາມະການອອກສຽງເລັຟຄວາມຮັກຄົນຮັກຄູ່ຮັກສິ່ງທີ່ເຈົ້າຮັກທີ່ຮັກເທັນນິດສູນຮັກຜູ້ໃດຜູ້ໜຶ່ງຮ່ວມປະເວນີປະເພດຄຳນາມການອອກສຽງເລັຟ 
  • using 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)) ຮັກມັກຫຼາຍຢາກໄດ້ຫຼາຍຢາກເຮັດຫຼາຍປະເພດຄໍາກໍາມະການອອກສຽງເລັຟຄວາມຮັກຄົນຮັກຄູ່ຮັກສິ່ງທີ່ເຈົ້າຮັກທີ່ຮັກເທັນນິດສູນຮັກຜູ້ໃດຜູ້ໜຶ່ງຮ່ວມປະເວນີປະເພດຄຳນາມການອອກສຽງເລັຟ 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -