Regular Expression IGNORECASE different for findall and split?

C

Chris

hello,
I have question about the re.I option for Regular Expressions:
['x', 'X']

as expected finds both lower and uppercase x

>>> re.split('x', '1x2X3', re.I) ['1', '2X3']
>>> re.split('x', '1x2X3')
['1', '2X3']

I expected ['1', '2', '3'] but in this case re.I bahaves exactly as not
present at all...

Is that an expected behaviour or a fault?
Running Python 2.4.1 on Windows XP

thanks for any hint
chris
 
P

Peter Otten

Chris said:
['1', '2X3']

I expected ['1', '2', '3'] but in this case re.I bahaves exactly as not
present at all...
Is that an expected behaviour or a fault?

This is expected:
Help on function split in module sre:

split(pattern, string, maxsplit=0)
Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings.

You are setting maxsplit to
2

Use re.compile() to get the desired behaviour:
['1', '2', '3']

Peter
 
C

Chris

Peter said:
Chris wrote:

re.split('x', '1x2X3', re.I)
['1', '2X3']


I expected ['1', '2', '3'] but in this case re.I bahaves exactly as not
present at all...


Is that an expected behaviour or a fault?


This is expected:


Help on function split in module sre:

split(pattern, string, maxsplit=0)
Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings.

You are setting maxsplit to


2

Use re.compile() to get the desired behaviour:


['1', '2', '3']

Peter

thanks, I should read the docs but

but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)

thanks
chris
 
S

Steven Bethard

Chris said:
but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match',
'scanner', 'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)

Use getattr:

method = 'split'
result = getattr(re.compile(REGEX), method)(TXT)

HTH,

STeVe
 
F

Fredrik Lundh

Chris said:
but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)

I really don't understand why you think you have to write
your RE code that way, but the mechanism you're looking
for is getattr:

result = getattr(r, method)(TXT)

</F>
 
C

Chris

Fredrik said:
but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)


I really don't understand why you think you have to write
your RE code that way, but the mechanism you're looking
for is getattr:

result = getattr(r, method)(TXT)

thanks (also to Steven) for the info, that is exactly what i was looking
for.

reason is that I built a small UI in which the user may choose if he
want to do a split, findall (and maybe later others like match or
search). So the method name comes in "from the UI". I could of course
use if/elif/else blocks but thought getattr should be shorter and
easier. I was not really aware of getattr which I was looking for on
other occations before...

chris
 
M

Mike Meyer

Chris said:
Fredrik said:
Chris said:
but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)
I really don't understand why you think you have to write
your RE code that way, but the mechanism you're looking
for is getattr:
result = getattr(r, method)(TXT)

thanks (also to Steven) for the info, that is exactly what i was
looking for.

reason is that I built a small UI in which the user may choose if he
want to do a split, findall (and maybe later others like match or
search). So the method name comes in "from the UI". I could of course
use if/elif/else blocks but thought getattr should be shorter and
easier. I was not really aware of getattr which I was looking for on
other occations before...

So why is the UI returning strings, instead of code objects of some
kind?

<mike
 
C

Chris

Mike said:
Chris said:
Fredrik said:
but more of a basic question following, I was doing the following before:

method = 'split' # came from somewhere else of course
result = re.__dict__[method].(REGEX, TXT)

precompiling the regex

r = compile(REGEX)

does give an regex object which has the needed methods

print dir(r)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
'search', 'split', 'sub', 'subn']

but how do I evaluate them without explicitly calling them?

result = r.__???MAGIC???__[method](TXT)

obviously I am not a Python pro ;)

I really don't understand why you think you have to write
your RE code that way, but the mechanism you're looking
for is getattr:
result = getattr(r, method)(TXT)

thanks (also to Steven) for the info, that is exactly what i was
looking for.

reason is that I built a small UI in which the user may choose if he
want to do a split, findall (and maybe later others like match or
search). So the method name comes in "from the UI". I could of course
use if/elif/else blocks but thought getattr should be shorter and
easier. I was not really aware of getattr which I was looking for on
other occations before...


So why is the UI returning strings, instead of code objects of some
kind?

<mike

it is a simple ajax call to a python server doing the re. maybe a bit
contrived but it was nice to try python/ajax on a for me useful app to
enable easy tryout of regular expressions. if you are interested check
out http://cthedot.de/retest/

chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top