473,746 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

f*cking re module

arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!

This is the problem:
import re
str = "blabla<python> Re modules sucks!</python>blabla"
re.search("(<py thon>)(/python>)", str).group()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'

the only thing I want are the number of places blabla, Re modules
sucks! and blabla are.

Noud

Jul 21 '05 #1
37 2999
jwaixs wrote:
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!


Then Google for regular expression tutorials, not regular expression
references.
import re
str = "blabla<python> Re modules sucks!</python>blabla"
re.search("(<py thon>)(/python>)", str).group()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'

the only thing I want are the number of places blabla, Re modules
sucks! and blabla are.


Your question is still not clear. What you're searching for is
'<python></python>', which isn't there, so .search returns None, and so
you get that exception (.group takes an argument, anyway).

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
With such a weapon I could boil the Earth to vapor.
-- Chmeee
Jul 21 '05 #2
On 4 Jul 2005 01:04:47 -0700, jwaixs <jw****@gmail.c om> wrote:
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found!


http://www.amk.ca/python/howto/regex/

--
Cheers,
Simon B,
si***@brunningo nline.net,
http://www.brunningonline.net/simon/blog/
Jul 21 '05 #3
jwaixs (04.07.2005 10:04):
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found!


Did you try this one?
http://www.amk.ca/python/howto/regex/regex.html
import re
str = "blabla<python> Re modules sucks!</python>blabla"
re.search(" (<python>)(/python>)", str).group()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'


RE doesn't find "(<python>) (/python>)" because it isn't in your string.
That's why group fails.
import re
s = "blabla<python> Re module is great!</python>blabla"
re.search("(<py thon>).*(/python>)", s).group()

'<python>Re module is great!</python>'
Matthias
Jul 21 '05 #4
jwaixs wrote:
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!


If you want to try out re interactively you could use:

<Python>\Tools\ Scripts\redemo. py
--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 21 '05 #5
jwaixs wrote:
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!

This is the problem:

import re
str = "blabla<python> Re modules sucks!</python>blabla"
re.search(" (<python>)(/python>)", str).group()


Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'

the only thing I want are the number of places blabla, Re modules
sucks! and blabla are.


Others gave you advice on how to deal withe regexes. I'm going to add
that regexes aren't the way to go for this - use HTMLParser. With your
regex, you won't be able to handle correctly either this

<foo>some text</foo><foo>some other text</foo>

as you will get the whole string, not just the first match. You can
alter the so-called longest match behaviour, but then

<foo>some oute text <foo>some inner text</foo> some more outer text</foo>
won't work....
Try and do not use regexps. Or at least do it in a way that you tokenize
the text and then can sweep over it collecting the data you need
yourself (but that's basically rewriting the html parsers out there).

Diez
Jul 21 '05 #6
try:
re.search("(<py thon>)(/python>)", str).group()
except:
print 'not found'

otherwise,

re.search("(<py thon>).*?(\/python>)", str).group()

this is will help!

Regards,
Gurpreet Singh

Blogging [at] http://garrythegambler.blogspot.com

On 4 Jul 2005 01:04:47 -0700, jwaixs <jw****@gmail.c om> wrote:
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!

This is the problem:
import re
str = "blabla<python> Re modules sucks!</python>blabla"
re.search("(<py thon>)(/python>)", str).group()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'

the only thing I want are the number of places blabla, Re modules
sucks! and blabla are.

Noud

--
http://mail.python.org/mailman/listinfo/python-list

--
Thanks and Regards,
GSS
Jul 21 '05 #7

"jwaixs" <jw****@gmail.c om> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
arg... I've lost 1.5 hours of my precious time to try letting re work
correcty. There's really not a single good re tutorial or documentation
I could found! There are only reference, and if you don't know how a
module work you won't learn it from a reference!


PowerGrep is a commercial Windows tool. However, it comes
with a manual that has a 45 page tutorial on regular expressions.
www.powergrep.com/manual/PowerGREP.pdf tutorial: pages 109-156
There is also a new Wrox book besides the O'Reilly/Friedl Owl book.

http://www.uhacc.org/tech_docs/guides/regex1.php
also regex2.php, and regex3.php

Only the delimiters have been changed to protect the innocent,
Stephen
Jul 21 '05 #8
Thank you for your replies, it's much obvious now. I know more what I
can and can't do with the re module. But is it possible to search for
more than one string in the same line?

bv. I want to replace the <python> with " "
</python> with "\n" and every thing that's not between the two python
tags must begin with "\nprint \"\"\"" and end with "\"\"\"\n"? Or do I
need more than one call?

Jul 21 '05 #9
If you are looking for HTML tags or something like that. Have a look
at the HTMLParser (docs.python.or g).

On 4 Jul 2005 03:37:02 -0700, jwaixs <jw****@gmail.c om> wrote:
Thank you for your replies, it's much obvious now. I know more what I
can and can't do with the re module. But is it possible to search for
more than one string in the same line?

bv. I want to replace the <python> with " "
</python> with "\n" and every thing that's not between the two python
tags must begin with "\nprint \"\"\"" and end with "\"\"\"\n"? Or do I
need more than one call?

--
http://mail.python.org/mailman/listinfo/python-list

Jul 21 '05 #10

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

Similar topics

8
3864
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
5
2017
by: dody suria wijaya | last post by:
I found this problem when trying to split a module into two. Here's an example: ============== #Module a (a.py): from b import * class Main: pass ============== ==============
3
16515
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or something in PHP that I don't have? Do I need to install more Linux packages? Or adjust PHP in some way?
10
5492
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in advance
21
34430
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
40
3484
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: AttributeError: 'module' object has no attribute 'handlers' The only experience I have in modules is os and os.path ... if I do the same thing, simply import os and then type dir(os.path), it displays the contents as expected.
4
2181
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: cwd = data os.chdir(cwd) print os.getcwd()
0
2164
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so that only means that the name is indeed available in the global scope.
6
2268
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization can be of use to me in my project. I'm currently messing with a problem where I'm keeping my global variables ( or symbols) in a module and the other mdoules in the project acess these global variables.
0
9351
jinu1996
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...
1
9286
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9219
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 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...
0
8229
agi2029
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...
1
6774
isladogs
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...
0
6062
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();...
0
4840
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2200
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.