473,769 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python and Cyrillic characters in regular expression

Hi, I'm trying extract all alphabetic characters from string.

reg = re.compile('(?u )([\w\s]+)', re.UNICODE)
buf = re.match(string )

But it's doesn't work. If string starts from Cyrillic character, all
works fine. But if string starts from Latin character, match returns
only Latin characters.

Please, help.
Sep 4 '08 #1
4 7261
phasma wrote:
Hi, I'm trying extract all alphabetic characters from string.

reg = re.compile('(?u )([\w\s]+)', re.UNICODE)
buf = re.match(string )

But it's doesn't work. If string starts from Cyrillic character, all
works fine. But if string starts from Latin character, match returns
only Latin characters.
can you provide a few sample strings that show this behaviour?

</F>

Sep 4 '08 #2
string = u"ðÒÉ×ÅÔ"
(u'\u041f\u0440 \u0438\u0432\u0 435\u0442',)

string = u"Hi.ðÒÉ×ÅÔ"
(u'Hi',)

On Sep 4, 9:53špm, Fredrik Lundh <fred...@python ware.comwrote:
phasma wrote:
Hi, I'm trying extract all alphabetic characters from string.
reg = re.compile('(?u )([\w\s]+)', re.UNICODE)
buf = re.match(string )
But it's doesn't work. If string starts from Cyrillic character, all
works fine. But if string starts from Latin character, match returns
only Latin characters.

can you provide a few sample strings that show this behaviour?

</F>
Sep 5 '08 #3
On Sep 5, 12:28*pm, phasma <xpa...@gmail.c omwrote:
string = u"ðÒÉ×ÅÔ"
All the characters are letters.
(u'\u041f\u0440 \u0438\u0432\u0 435\u0442',)

string = u"Hi.ðÒÉ×ÅÔ"
The third character isn't a letter and isn't whitespace.
(u'Hi',)
On Sep 4, 9:53špm, Fredrik Lundh <fred...@python ware.comwrote:
phasma wrote:
Hi, I'm trying extract all alphabetic characters from string.
reg = re.compile('(?u )([\w\s]+)', re.UNICODE)
buf = re.match(string )
But it's doesn't work. If string starts from Cyrillic character, all
works fine. But if string starts from Latin character, match returns
only Latin characters.
can you provide a few sample strings that show this behaviour?
Sep 5 '08 #4
phasma wrote:
string = u"ðÒÉ×ÅÔ"
(u'\u041f\u0440 \u0438\u0432\u0 435\u0442',)

string = u"Hi.ðÒÉ×ÅÔ"
(u'Hi',)
the [\w\s] pattern you used matches letters, numbers, underscore, and
whitespace. "." doesn't fall into that category, so the "match" method
stops when it gets to that character.

maybe you could use re.sub or re.findall?
>># replace all non-alphanumerics with the empty string
re.sub("(?u)\ W+", "", string)
u'Hi\u041f\u044 0\u0438\u0432\u 0435\u0442'
>># find runs of alphanumeric characters
re.findall("( ?u)\w+", string)
[u'Hi', u'\u041f\u0440\ u0438\u0432\u04 35\u0442']
>>"".join(re.fi ndall("(?u)\w+" , string))
u'Hi\u041f\u044 0\u0438\u0432\u 0435\u0442'

(the "sub" example expects you to specify what characters you want to
skip, while "findall" expects you to specify what you want to keep.)

</F>

Sep 5 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
4669
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form UNQUOTE
35
6327
by: Philipp Lenssen | last post by:
Does anybody have experience displaying Cyrillic in common browsers with common settings? I found the following page researching the topic, however I cannot display all characters in the table (IE6, either automatic encoding detection, or forced Cyrillic encoding): <http://www.alanwood.net/unicode/cyrillic.html> For example, the first character in the table ("Cyrillic Capital Letter
8
3670
by: Kirill Simonov | last post by:
Hi, Could anyone suggest me a simple IDE suitable for teaching Python as a first programming language to high school students? It is necessary that it has a good support for input/output in Cyrillic. Unfortunately, most IDEs I tried failed miserably in this respect. My test was simple: I've run the code name = raw_input("What's your name? ") # written in Russian print "Hello, %s!" % name # in Russian as well
3
10201
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class. This is not a regular expression tutorial. Assumes you are already familiar with basic regular expression concepts and terminology. If not, you may want to read some regular expression tutorial. See the end of the article for links to online resources....
5
5372
by: Just Another Victim of the Ambient Morality | last post by:
This won't compile for me: regex = re.compile('(.*\\).*') I get the error: sre_constants.error: unbalanced parenthesis
0
2054
by: yrogirg | last post by:
Actually, I need utf-8 to utf-8 encoding which would change the text to another keyboard layout (e.g. from english to russian ghbdtn -> ÐÒÉ×ÅÔ) and would not affect other symbols. I`m totally new to python and to more or less advanced programming. I couldn`t find the answer to the question anywhere. I`ve tried create simple utf to utf codec for some symbols but it doesn`t work. Here it is.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.