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 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.
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 ==...
|
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...
|
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
|
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() {}...
|
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...
|
by: wong_powah |
last post by:
#include <vector>
#include <iostream>
using std::cout;
using std::vector;
enum {DATASIZE = 20};
typedef unsigned char data_t;
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |