Regular Expressions to Replace Strings 
Author Message
 Regular Expressions to Replace Strings

I'm trying to open a text file and do a search and replace that is case
insensitive and replaces all occurances.  I have the following code, but it
has problems if the word exists in the string.  Does anybody have any ideas
what I'm doing wrong.

PageRoot="../pages/contactus.ptm"
UseRoot=open(PageRoot, "r").read()
UseWord="python"
pat=re.compile(UseWord,re.I)
UseRoot=pat.sub('<B>\\1</B>', UseRoot)

The error I get is unknown group number.    Thanks in advance. I want to use
this code as part of my search engine, so the word will always be a
variable.

Colin



Sun, 02 Nov 2003 21:58:39 GMT  
 Regular Expressions to Replace Strings

CM> I'm trying to open a text file and do a search and replace that is case
CM> insensitive and replaces all occurances.  I have the following code, but it
CM> has problems if the word exists in the string.  Does anybody have any ideas
CM> what I'm doing wrong.

CM> PageRoot="../pages/contactus.ptm"
CM> UseRoot=open(PageRoot, "r").read()
CM> UseWord="python"
CM> pat=re.compile(UseWord,re.I)
CM> UseRoot=pat.sub('<B>\\1</B>', UseRoot)

CM> The error I get is unknown group number. Thanks in advance. I want to
CM> use this code as part of my search engine, so the word will always be a
CM> variable.

You have no group. To get one, use the r.e. "(python)".
--

URL: http://www.cs.uu.nl/~piet [PGP]



Sun, 02 Nov 2003 23:15:56 GMT  
 Regular Expressions to Replace Strings
Sorry to be dense, but how would I do that using my example?

Colin

Quote:


> CM> I'm trying to open a text file and do a search and replace that is
case
> CM> insensitive and replaces all occurances.  I have the following code,
but it
> CM> has problems if the word exists in the string.  Does anybody have any
ideas
> CM> what I'm doing wrong.

> CM> PageRoot="../pages/contactus.ptm"
> CM> UseRoot=open(PageRoot, "r").read()
> CM> UseWord="python"
> CM> pat=re.compile(UseWord,re.I)
> CM> UseRoot=pat.sub('<B>\\1</B>', UseRoot)

> CM> The error I get is unknown group number. Thanks in advance. I want to
> CM> use this code as part of my search engine, so the word will always be
a
> CM> variable.

> You have no group. To get one, use the r.e. "(python)".
> --

> URL: http://www.cs.uu.nl/~piet [PGP]




Sun, 02 Nov 2003 23:38:02 GMT  
 Regular Expressions to Replace Strings

Quote:

> I'm trying to open a text file and do a search and replace that is case
> insensitive and replaces all occurances.  I have the following code, but it
> has problems if the word exists in the string.  Does anybody have any ideas
> what I'm doing wrong.

> PageRoot="../pages/contactus.ptm"
> UseRoot=open(PageRoot, "r").read()
> UseWord="python"

You want parentheses around the search pattern so it get put in a "group".
So,

    >>> text = "ads flakjefae Pythonafeaef apythonasfae"
    >>> import re
    >>> pat = re.compile("(python)", re.I)
    >>> pat.sub('<B>\\1</B>', text)
    'ads flakjefae <B>Python</B>afeaef a<B>python</B>asfae'
    >>>

Quote:
> pat=re.compile(UseWord,re.I)
> UseRoot=pat.sub('<B>\\1</B>', UseRoot)

> The error I get is unknown group number.    Thanks in advance. I want to use
> this code as part of my search engine, so the word will always be a
> variable.

Cheers,
Trent

--
Trent Mick



Sun, 02 Nov 2003 23:39:37 GMT  
 Regular Expressions to Replace Strings

Quote:

> > CM> UseWord="python"
> > CM> pat=re.compile(UseWord,re.I)

Group 0 corresponds to the match as a whole, so you could use \0 in
your replacement pattern.  Alternatively, you could write '(python)'
instead, which puts your string inside a group, which would then be
numbered group 1, but that's a bit silly unless there are other
components of the regex pattern that you want to ignore (for example,
if there was a \s* to catch leading whitespace, and you didn't care
what the whitespace was).

--amk



Mon, 03 Nov 2003 00:03:04 GMT  
 Regular Expressions to Replace Strings

AK> Group 0 corresponds to the match as a whole, so you could use \0 in
AK> your replacement pattern.

In my python 2.0 \0 doesn't work. It just inserts a \0 character.
--

URL: http://www.cs.uu.nl/~piet [PGP]



Tue, 04 Nov 2003 19:15:33 GMT  
 Regular Expressions to Replace Strings

Quote:


> AK> Group 0 corresponds to the match as a whole, so you could use \0 in
> AK> your replacement pattern.

> In my python 2.0 \0 doesn't work. It just inserts a \0 character.

True (in both 2.0 and 2.1, it seems) -- you appear to need
the alternative \g<0> syntax, for some reason... this works:

import re

base = 'tanto va la gatta al lardo'
vow = re.compile('a|o')
resu = vow.sub(r'"\g<0>"', base)
print resu

Alex



Tue, 04 Nov 2003 20:53:07 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. regular expression to parse 0,xx/xx/xx and replace 0 in GPEP

2. Replace with regular expressions "except if..."

3. Replacing an expression with a different string...

4. VW String - regular expressions?

5. Regular Expression for Match Pattern (string) Function

6. Regular expression string pattern matching: Embedding pop-11 procedures, and more

7. Regular expressions from the end of a string

8. regular expressions and string#sub!

9. string regular expression matcher?

10. Regular Expression: Highlighting Strings but not in HTML tags

11. regular expressions with other string classes

12. Need code to match strings with regular expressions

 

 
Powered by phpBB® Forum Software