473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

python regex character group matches

hello python-list!

the other day, i was trying to match unicode character sequences that
looked like this:

\\uAD0X...

my issue, is that the pattern i used was returning:

[ '\\uAD0X', '\\u1BF3', ... ]

when i expected:

[ '\\uAD0X\\u1BF3', ]

the code looks something like this:

pat = re.compile("(\\\u[0-9A-F]{4})+", re.UNICODE|re.LOCALE)
#print pat.findall(txt_line)
results = pat.finditer(txt_line)

i ran the pattern through a couple of my colleagues and they were all
in agreement that my pattern should have matched correctly.

is this a simple case of a messed up regex or am i not using the regex
api correctly?

cheers,

ct
Sep 17 '08 #1
2 1838
On Wed, 17 Sep 2008 09:27:47 -0400, christopher taylor wrote:
the other day, i was trying to match unicode character sequences that
looked like this:

\\uAD0X...

my issue, is that the pattern i used was returning:

[ '\\uAD0X', '\\u1BF3', ... ]

when i expected:

[ '\\uAD0X\\u1BF3', ]

the code looks something like this:

pat = re.compile("(\\\u[0-9A-F]{4})+", re.UNICODE|re.LOCALE) #print
pat.findall(txt_line)
results = pat.finditer(txt_line)

i ran the pattern through a couple of my colleagues and they were all in
agreement that my pattern should have matched correctly.
Correctly for what input? And the examples above are not matching (no
pun intended) the regular expression. `pat` doesn't match '\\uAD0X'
because there's no 'X' in the character class. BTW: Are you sure you
need or want the `re.UNICODE` flag?

Ciao,
Marc 'BlackJack' Rintsch
Sep 17 '08 #2
On Wed, 17 Sep 2008 09:27:47 -0400, christopher taylor wrote:
hello python-list!

the other day, i was trying to match unicode character sequences that
looked like this:

\\uAD0X...
It is not clear what this is supposed to be. Is that matching a literal
pair of backslashes, or a single escaped backslash, or a single unicode
character with codepoint AD0X, or what?

If I read it *literally*, then you're trying to match:

backslash backslash lowercase-u uppercase-A uppercase-D zero uppercase-X

Is that what you intended to match?

my issue, is that the pattern i used was returning:

[ '\\uAD0X', '\\u1BF3', ... ]
Unless you are using Python 3, I see that you aren't actually dealing
with Unicode strings, you're using byte strings. Is that deliberate?

when i expected:

[ '\\uAD0X\\u1BF3', ]
I make that to be a string of length 12. Is that what you are expecting?
>>len('\\uAD0X\\u1BF3')
12

the code looks something like this:

pat = re.compile("(\\\u[0-9A-F]{4})+", re.UNICODE|re.LOCALE)
#print pat.findall(txt_line)
results = pat.finditer(txt_line)
First point: I don't think the UNICODE flag does what you think it does.
It redefines the meaning of special escape sequences \b etc. Since you
aren't using any special escape sequences, I'm going to guess that you
think it turns your search string into Unicode. It doesn't. (Apologies in
advance if I guessed wrong.) I don't think you need either the UNICODE or
LOCALE flag for this search: they don't seem to have any effect.

Secondly: you will generally save yourself a lot of trouble when writing
regexes to use raw strings, because backslashes in the regex engine clash
with backslashes in Python strings. But there's a gotcha: backslash
escapes behave differently in ordinary strings and the re engine.

In an ordinary string, the sequence backslash-char is treated as a
literal backslash-char if it isn't a special escape. So:
>>len('\t') # special escape
1
>>len('\u') # not a special escape
2

But that's not the case in the re engine! As the Fine Manual says:

The special sequences consist of "\" and a character from
the list below. If the ordinary character is not on the
list, then the resulting RE will match the second character.
For example, \$ matches the character "$".

http://docs.python.org/lib/re-syntax.html

So all of these match the same thing:
re.compile('\\u')
re.compile(r'\u')
re.compile('u')

To match a literal backslash-u, you need to escape the backslash before
the engine joins it to the u: r'\\u'.

Putting it all together again:

pat = re.compile(r"(\\u[0-9A-F]{4})+")

will probably do what you want, assuming I have guessed what you want
correctly!

--
Steven
Sep 17 '08 #3

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

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
2
by: Andrew Robert | last post by:
I have two Perl expressions If windows: perl -ple "s/()/sprintf(q#%%%2X#, ord $1)/ge" somefile.txt If posix perl -ple 's/()/sprintf("%%%2X", ord $1)/ge' somefile.txt
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
7
by: MrNobody | last post by:
I'm trying to do some regex in C# but for some reason linebreaks are causing my regex to not work. the test string goes like this: string ss = "<tagname...
4
by: unexpected | last post by:
I'm trying to do a whole word pattern match for the term 'MULTX-' Currently, my regular expression syntax is: re.search(('^')+(keyword+'\\b') where keyword comes from a list of terms....
6
by: Johny | last post by:
Playing a little more with strings, I found out that string.find function provides the position of the first occurance of the substring in the string. Is there a way how to find out all...
11
by: proctor | last post by:
hello, i have a regex: rx_test = re.compile('/x()*x/') which is part of this test program: ============ import re
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.