Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49274

Re: re.finditer() skips unicode into selection

From Terry Reedy <tjreedy@udel.edu>
Subject Re: re.finditer() skips unicode into selection
Date 2013-06-26 16:14 -0400
References <1d412b4a-b043-4723-909a-54c6c06cebd4@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3899.1372277713.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 6/26/2013 3:18 PM, akshay.ksth@gmail.com wrote:
> I am using the following Highlighter class for Spell Checking to work on my QTextEdit.
>
> class Highlighter(QSyntaxHighlighter):
>      pattern = ur'\w+'
>      def __init__(self, *args):
>          QSyntaxHighlighter.__init__(self, *args)
>          self.dict = None
>
>      def setDict(self, dict):
>          self.dict = dict
>
>      def highlightBlock(self, text):
>          if not self.dict:
>              return
>          text = unicode(text)
>          format = QTextCharFormat()
>          format.setUnderlineColor(Qt.red)
>          format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
>          unicode_pattern=re.compile(self.pattern,re.UNICODE|re.LOCALE)
>
>          for word_object in unicode_pattern.finditer(text):
>              if not self.dict.spell(word_object.group()):
>                  print word_object.group()
>                  self.setFormat(word_object.start(), word_object.end() - word_object.start(), format)
>
> But whenever I pass unicode values into my QTextEdit the re.finditer() does not seem to collect it.
>
> When I pass "I am a नेपाली" into the QTextEdit. The output is like this:
>
>      I I I a I am I am I am a I am a I am a I am a I am a I am a I am a I am a
>
> It is completely ignoring the unicode.

The whole text is unicode. It is ignoring the non-ascii, as you asked it 
to with re.LOCALE.

With 3.3.2:
import re

pattern = re.compile(r'\w+', re.LOCALE)
text = "I am a नेपाली"

for word in pattern.finditer(text):
     print(word.group())
 >>>
I
am
a

Delete ', re.LOCALE' and the following are also printed:
न
प
ल

There is an issue on the tracker about the vowel marks in नेपाली being 
mis-seen as word separators, but that is another issue.

Lesson: when you do not understand output, simplify code to see what 
changes. Separating re issues from framework issues is a big step in 
that direction.

? What might be the issue. I am new to PyQt and regex. Im using Python 
2.7 and PyQt4.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

re.finditer() skips unicode into selection akshay.ksth@gmail.com - 2013-06-26 12:18 -0700
  Re: re.finditer() skips unicode into selection Terry Reedy <tjreedy@udel.edu> - 2013-06-26 16:14 -0400
  Re: re.finditer() skips unicode into selection MRAB <python@mrabarnett.plus.com> - 2013-06-26 21:24 +0100
  Re: re.finditer() skips unicode into selection darpan6aya <akshay.ksth@gmail.com> - 2013-06-26 20:26 -0700
    Re: re.finditer() skips unicode into selection darpan6aya <akshay.ksth@gmail.com> - 2013-06-26 20:31 -0700
      Re: re.finditer() skips unicode into selection darpan6aya <akshay.ksth@gmail.com> - 2013-06-26 20:39 -0700
        Re: re.finditer() skips unicode into selection darpan6aya <akshay.ksth@gmail.com> - 2013-06-26 21:25 -0700

csiph-web