By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

Regular Expression IGNORECASE different for findall and split?

P: n/a
hello,
I have question about the re.I option for Regular Expressions:
import re
re.findall('x', '1x2X3', re.I) ['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

Sep 6 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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(re.split) 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
re.I 2

Use re.compile() to get the desired behaviour:
re.compile("x", re.I).split("1x2X3")

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

Peter
Sep 6 '05 #2

P: n/a
Peter Otten wrote:
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(re.split)
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

re.I
2

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

re.compile("x", re.I).split("1x2X3")


['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
Sep 6 '05 #3

P: n/a
Chris wrote:
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
Sep 6 '05 #4

P: n/a
Chris <c@cdot.de> wrote:
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>

Sep 6 '05 #5

P: n/a
Fredrik Lundh wrote:
Chris <c@cdot.de> wrote:

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
Sep 7 '05 #6

P: n/a
Chris <c@cdot.de> writes:
Fredrik Lundh wrote:
Chris <c@cdot.de> wrote:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 7 '05 #7

P: n/a
Mike Meyer wrote:
Chris <c@cdot.de> writes:
Fredrik Lundh wrote:
Chris <c@cdot.de> wrote:
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
Sep 8 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.