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 7 2214
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("1x 2X3")
['1', '2', '3']
Peter
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.spl it) 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("1x 2X3")
['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
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.comp ile(REGEX), method)(TXT)
HTH,
STeVe
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>
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)
precompilin g 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
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)
precompili ng 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.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
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)
precompilin g 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: andrea.gavana |
last post by:
Hello NG,
I am quite new with Python... I'm writing an application that does
also some regexp things on strings, but I'm having problem about
identifying/extracting a substring from another string. What I have to do
is to extract all the strings that begins with a "$" character, but
excluding characters like "." (point) and "'" (single quote) and "\" "/"
(slashes). For example I have:
1) This Is An $EXAMPLE String
|
by: Chris Lasher |
last post by:
Hello,
I would like to create a set of very similar regular expression. In
my initial thought, I'd hoped to create a regular expression with a
variable inside of it that I could simply pass a string into by
defining this variable elsewhere in my module/function/class where I
compile the regular expression, thus making for an easy substitution.
However, still after JFGI'ing, I still cannot find the syntax to place
a variable inside a...
|
by: Michael McGarry |
last post by:
Hi,
I am horrible with Regular Expressions, can anyone recommend a book on it?
Also I am trying to parse the following string to extract the number
after load average.
".... load average: 0.04, 0.02, 0.01"
how can I extract this number with RE or otherwise?
|
by: Marius |
last post by:
Hi,
I'm having an odd problem with a regular expression in C#. I'm trying
to parse the rows in a web page with this pattern, and the computer
locks up:
"<tr valign=middle>.+?<a
href=\"/listings/details/index\\.cfm\\?itemnum=.+?8000 FB
24/7.+?(Antonius|Marcus).+?PLATINUM (\\d+)K
PP.+?\\$<b>(\\d+(\\.\\d+)*)</b>.+?</tr>"
|
by: Dan Schumm |
last post by:
I'm relatively new to regular expressions and was looking for some help on a
problem that I need to solve. Basically, given an HTML string, I need to
highlight certain words within the text of the string. I had it working
somewhat, but ran into problems if one of the highlighted words could also be
part of an HTML tag (such as 'Table' or 'Border'). What I need is the regex
to find the word, but ignore any words that fall between an HTML...
| |
by: Ernesto |
last post by:
I'm trying to get the right syntax for my regular expression. The
string I'm trying to parse is:
# myString
Name: David Dude
Right now, I'm using the following code:
|
by: oscartheduck |
last post by:
Hi folks,
I'm trying to alter a program I posted about a few days ago. It
creates thumbnail images from master images. Nice and simple.
To make sure I can match all variations in spelling of jpeg, and
different cases, I'm using regular expressions.
The code is currently:
|
by: Noah Hoffman |
last post by:
I have been trying to write a regular expression that identifies a
block of text enclosed by (potentially nested) parentheses. I've found
solutions using other regular expression engines (for example, my text
editor, BBEdit, which uses the PCRE library), but have not been able
to replicate it using python's re module.
Here's a version that works using the PCRE syntax, along with the
python error message. I'm hoping for this to identify...
|
by: scsoce |
last post by:
MRAB wrote:
Yes, you are right, but this way findall() capture only the 'top' group.
What I really need to do is to capture nested and repated patterns, say,
<tabletag in html contains many <tr>, <tr contains many <td>,
the data in <td is i need, so I write the regx like this:
regx ='''
<table.*\n
(
(\s*<tr.*\n
(\s*<td.*</td>\n|\n)*
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |