473,395 Members | 1,726 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,395 software developers and data experts.

re.compile versus r''

Hello, I'm using a tool (PLY) which apparently expects the tokens to
be created using r''

But because one token is a rather complex regular expression, I want
to create the regular expression programmatically.

How can I generate a string and then create something of the same type
that the r'' function does?

Concretely, in the program below, consonant is not the same type as
t_NAME, but I assume that it needs to be for PLY to use it for
tokenizing:

import re

t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'

guttural = 'kh?|gh?|\"n'
palatal = '(?:chh?|jh?|\~n)'
cerebral = '\.(?:th?|dh?|n)'
dental = '(?:th?|dh?|n)'
semivowel = '[yrlv]'
sibilant = '[\"\.]?s'
aspirant = 'h'

consonant = re.compile('|'.join([guttural , palatal , cerebral ,
dental , semivowel , sibilant , aspirant]))

print consonant
print t_NAME
Aug 28 '08 #1
3 1252
Oh my god, how embarrassing. the r'' notation is to create raw string
<http://www.swc.scipy.org/lec/glossary.html#gdef-raw_string>

I thought it was some form of blessing a string into a regular
expression class.

Aug 28 '08 #2
Terrence Brannon wrote:
Hello, I'm using a tool (PLY) which apparently expects the tokens to
be created using r''

But because one token is a rather complex regular expression, I want
to create the regular expression programmatically.

How can I generate a string and then create something of the same type
that the r'' function does?
r'' is an alternative syntax for string literals that affects how escape
sequences are interpreted; there's no separate string type for strings
created by this syntax.

</F>

Aug 28 '08 #3
On Thu, Aug 28, 2008 at 12:23 PM, Terrence Brannon <me******@gmail.comwrote:
Hello, I'm using a tool (PLY) which apparently expects the tokens to
be created using r''

But because one token is a rather complex regular expression, I want
to create the regular expression programmatically.

How can I generate a string and then create something of the same type
that the r'' function does?
The "r" prefix isn't a function or a type, it's merely a special
literal syntax for strings that's handy when you're writing regexes
and therefore have to deal with another level of backslash escaping.
See the second to last paragraph of
http://docs.python.org/ref/strings.html for more info.

Regards,
Chris
>
Concretely, in the program below, consonant is not the same type as
t_NAME, but I assume that it needs to be for PLY to use it for
tokenizing:

import re

t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'

guttural = 'kh?|gh?|\"n'
palatal = '(?:chh?|jh?|\~n)'
cerebral = '\.(?:th?|dh?|n)'
dental = '(?:th?|dh?|n)'
semivowel = '[yrlv]'
sibilant = '[\"\.]?s'
aspirant = 'h'

consonant = re.compile('|'.join([guttural , palatal , cerebral ,
dental , semivowel , sibilant , aspirant]))

print consonant
print t_NAME
--
http://mail.python.org/mailman/listinfo/python-list
Aug 28 '08 #4

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

Similar topics

11
by: Markus Dehmann | last post by:
I have a big class that contains code like this (the code is automatically generated according to some configuration): if(str == "name1"){ do; something; name1_specific; }else if(str ==...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
33
by: Joshua D. Drake | last post by:
Hello, I think the below just about says it all: http://www.commandprompt.com/images/mammoth_versus_dolphin_500.jpg Sincerely, Joshua Drake
2
by: Andrew Robinson | last post by:
I need to create a shared static field for use within a number of different classes. Which one should I be using or are they all really the same thing? public class Widget { private Widget() {}...
10
by: bob | last post by:
Hello, I use Microsoft Visual C++ .NET (version 7.1.3088) Sometimes (with big codes?) when I get a compile error and click on the error, the cursor is placed next to the wrong piece of code. The...
2
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
4
by: aj | last post by:
DB2 8.2 LUW FP14 Is there any real difference between select blahblahblah... where blah IN (select blah......) versus select blahblahblah... where blah = ANY (select blah.....) versus select...
0
by: stupid48 | last post by:
Ok, I'm just a server guy with a bit a VS 2005 experience. I know that you can deploy asp.net apps either compiled or not compiled. My developer says that he prefers not to compile. I have to...
20
by: KW | last post by:
I have an application that runs in any of Access 2000 thru Access 2007 to accommodate my customers' various environments. I implemented code to call a function that uses the LoadCustomUI method to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.