Re: Passing info to function used in re.sub

2023-09-05 Thread Jan Erik Moström via Python-list
On 3 Sep 2023, at 18:10, Jan Erik Moström via Python-list wrote: > I'm looking for some advice for how to write this in a clean way Thanks for all the suggestion, I realize that I haven't written Python code in a while. I should have remembered this myself !!! Thanks for reminding me. = jem --

Re: Passing info to function used in re.sub

2023-09-04 Thread Dieter Maurer via Python-list
Jan Erik Moström wrote at 2023-9-3 18:10 +0200: >I'm looking for some advice for how to write this in a clean way > ... >The "problem" is that I've currently written some code that works but it uses >global variables ... and I don't like global variables. I assume there is a >better way to write

Re: Passing info to function used in re.sub

2023-09-04 Thread Peter J. Holzer via Python-list
# Do various things that involves for info > # that what's available in m > replacement_text = m.group(1) + global_var1 + global_var2 > return replacement_text > > and the call comes here > > global_var1 = "bla bla" > global_var2 =

Re: Passing info to function used in re.sub

2023-09-03 Thread Thomas Passin via Python-list
r2 = "pff" new_text = re.sub(im_pattern,fix_stuff,md_text) The "problem" is that I've currently written some code that works but it uses global variables ... and I don't like global variables. I assume there is a better way to write this, but how? = jem There are tw

Re: Passing info to function used in re.sub

2023-09-03 Thread Jan Erik Moström via Python-list
On 3 Sep 2023, at 19:13, MRAB via Python-list wrote: > You could use pass an anonymous function (a lambda) to re.sub: Of course !! Thanks. = jem -- https://mail.python.org/mailman/listinfo/python-list

Re: Passing info to function used in re.sub

2023-09-03 Thread MRAB via Python-list
like this: def fix_stuff(m): # Do various things that involves for info # that what's available in m replacement_text = m.group(1) + global_var1 + global_var2 return replacement_text and the call comes here global_var1 = "bla bla" global_var2 = &quo

Passing info to function used in re.sub

2023-09-03 Thread Jan Erik Moström via Python-list
that involves for info # that what's available in m replacement_text = m.group(1) + global_var1 + global_var2 return replacement_text and the call comes here global_var1 = "bla bla" global_var2 = "pff" new_text = re.sub(im_pattern,fix_stuff,md_text

Re: I can't understand re.sub

2015-12-01 Thread Erik
On 01/12/15 05:28, Jussi Piitulainen wrote: A real solution should be aware of the actual structure of those lines, assuming they follow some defined syntax. I think that we are in violent agreement on this ;) E. -- https://mail.python.org/mailman/listinfo/python-list

Re: I can't understand re.sub

2015-11-30 Thread Jussi Piitulainen
that's not what the OP said they wanted to do. They said > everything was very fixed - they did not want a general purpose human > language text processing solution ... ;) Language processing is not what I had in mind here. Merely this, that there is some sort of word boundary, be i

Re: I can't understand re.sub

2015-11-30 Thread Erik
On 30/11/15 08:51, Jussi Piitulainen wrote: Surely the straight thing to say is: >>> foo.replace(' CONTENT_PATH ', ' Substitute ') 'foo bar baz spam Substitute bar spam' Not quite the same thing (but yes, with a third argument of 1, it would be). But there was no guarantee of spaces

Re: I can't understand re.sub

2015-11-30 Thread Jussi Piitulainen
Erik writes: > On 29/11/15 21:36, Mr Zaug wrote: >> This should be simple, right? > > It is. And it could be even simpler if you don't bother with regexes > at all (if your input is as fixed as you say it is): > > >>> foo = "foo bar baz spam CONTENT_PATH bar spam" > >>> ' Substitute '.join(foo.spl

Re: I can't understand re.sub

2015-11-30 Thread Erik
On 29/11/15 21:36, Mr Zaug wrote: I need to use re.sub to replace strings in a text file. Do you? Is there any other way? result = re.sub(pattern, repl, string, count=0, flags=0); I think I understand that pattern is the regex I'm searching for and repl is the thing I want to substitut

Re: I can't understand re.sub

2015-11-29 Thread Mr Zaug
On Sunday, November 29, 2015 at 8:12:25 PM UTC-5, Rick Johnson wrote: > On Sunday, November 29, 2015 at 3:37:34 PM UTC-6, Mr Zaug wrote: > > > The items I'm searching for are few and they do not change. They are > > "CONTENT_PATH", "ENV" and "NNN". These appear on a few lines in a template > > f

Re: I can't understand re.sub

2015-11-29 Thread Mr Zaug
Thanks. That does help quite a lot. -- https://mail.python.org/mailman/listinfo/python-list

Re: I can't understand re.sub

2015-11-29 Thread Rick Johnson
On Sunday, November 29, 2015 at 3:37:34 PM UTC-6, Mr Zaug wrote: > The items I'm searching for are few and they do not change. They are > "CONTENT_PATH", "ENV" and "NNN". These appear on a few lines in a template > file. They do not appear together on any line and they only appear once on > eac

Re: I can't understand re.sub

2015-11-29 Thread Denis McMahon
On Sun, 29 Nov 2015 13:36:57 -0800, Mr Zaug wrote: > result = re.sub(pattern, repl, string, count=0, flags=0); re.sub works on a string, not on a file. Read the file to a string, pass it in as the string. Or pre-compile the search pattern(s) and process the file line by line: import re pa

I can't understand re.sub

2015-11-29 Thread Mr Zaug
I need to use re.sub to replace strings in a text file. I can't seem to understand how to use the re module to this end. result = re.sub(pattern, repl, string, count=0, flags=0); I think I understand that pattern is the regex I'm searching for and repl is the thing I want to subs

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-14 Thread scottcabit
On Tuesday, May 13, 2014 4:26:51 PM UTC-4, MRAB wrote: > > 0x96 is a hexadecimal literal for an int. Within a string you need \x96 > > (it's \x for 2 hex digits, \u for 4 hex digits, \U for 8 hex digits). Yes, that was my problem. Figured it out just after posting my last message. using \x96

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-14 Thread alister
a hex editor and *hopefully* you will be able >> >> to >> >> >> see chunks of human-readable text where you can identify how >> >> en-dashes >> >> >> and similar are stored. >> >> >> > >> >I created a .d

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread wxjmfauth
> > > > >I created a .doc file and opened it with UltraEdit in binary (Hex) mode. > > What I see is that there are two characters, one for ndash and one for > > mdash, each a single byte long. 0x96 and 0x97. > > >So I tried this: fStr = re.sub(b'\0

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread MRAB
dashes and similar are stored. I created a .doc file and opened it with UltraEdit in binary (Hex) mode. What I see is that there are two characters, one for ndash and one for mdash, each a single byte long. 0x96 and 0x97. So I tried this: fStr = re.sub(b'\0x96',b'-',fStr)

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread scottcabit
gt; and similar are stored. I created a .doc file and opened it with UltraEdit in binary (Hex) mode. What I see is that there are two characters, one for ndash and one for mdash, each a single byte long. 0x96 and 0x97. So I tried this: fStr = re.sub(b'\0x96',b'-',fStr)

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread Chris Angelico
On Tue, May 13, 2014 at 11:49 PM, Steven D'Aprano wrote: > > This {EN DASH} is an n-dash. > > or: > > x\x9c\x0b\xc9\xc8,V\xa8v\xf5Spq\x0c\xf6\xa8U\x00r\x12 > \xf3\x14\xf2tS\x12\x8b3\xf4\x00\x82^\x08\xf8 > > > (that last one is the text passed through the zlib compressor) I had to deco

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread Steven D'Aprano
On Mon, 12 May 2014 10:35:53 -0700, scottcabit wrote: > On Friday, May 9, 2014 8:12:57 PM UTC-4, Steven D'Aprano wrote: > >> Good: >> >> >> >> fStr = re.sub(b'‒', b'-', fStr) >> >> > Doesn't work...the

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-13 Thread Dave Angel
On 05/12/2014 01:35 PM, scottca...@gmail.com wrote: On Friday, May 9, 2014 8:12:57 PM UTC-4, Steven D'Aprano wrote: Good: # Untested fStr = re.sub(b'&#x(201[2-5])|(2E3[AB])|(00[2A]D)', b'-', fStr) Still doesn't work. Guess whatever th

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-12 Thread Rustom Mody
On Monday, May 12, 2014 11:05:53 PM UTC+5:30, scott...@gmail.com wrote: > On Friday, May 9, 2014 8:12:57 PM UTC-4, Steven D'Aprano wrote: > > fStr = fStr.replace(b'‒', b'-') > >Still doesn't work > > > > Best: > > > >

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-12 Thread scottcabit
On Friday, May 9, 2014 8:12:57 PM UTC-4, Steven D'Aprano wrote: > Good: > > > > fStr = re.sub(b'‒', b'-', fStr) > Doesn't work...the document has been verified to contain endash and emdash characters, but this does NOT replace them. >

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-10 Thread Tim Golden
On 10/05/2014 08:11, wxjmfa...@gmail.com wrote: Anyway, as Python may fail as soon as one uses an EM DASH or an EM DASH, I think it's not worth the effort to spend to much time with it. Nope -- seems all right to me. (Hopefully helping the OP out as well as rebutting a rather foolish assertion

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-10 Thread wxjmfauth
Le samedi 10 mai 2014 06:22:00 UTC+2, Rustom Mody a écrit : > On Saturday, May 10, 2014 1:21:04 AM UTC+5:30, scott...@gmail.com wrote: > > > Hi, > > > > > > > > > > > > here is a snippet of code that opens a file (fn contains the path\name) > > and first tried to replace all endash, emdas

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread Rustom Mody
On Saturday, May 10, 2014 1:21:04 AM UTC+5:30, scott...@gmail.com wrote: > Hi, > > > > here is a snippet of code that opens a file (fn contains the path\name) and > first tried to replace all endash, emdash etc characters with simple dash > characters, before doing a search. > > But the re

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread Steven D'Aprano
d with "\x00". > > Hmmm..thought that was what I was doing. Can anyone figure out why the > syntax is wrong for Word 2007 document binary file data? You are searching for the literal "‒", in other words: ampersand hash x two zero one two *not* a FIGURE DASH

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread Steven D'Aprano
string replacement. > fn = 'z:\Documentation\Software' > def processdoc(fn,outfile): > fStr = open(fn, 'rb').read() > re.sub(b'‒','-',fStr) Good: fStr = re.sub(b'‒', b'-', fStr) Better: fStr = fStr.replace(b&#x

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread scottcabit
On Friday, May 9, 2014 4:09:58 PM UTC-4, Tim Chase wrote: > A Word doc (as your subject mentions) is a binary format. There's > the older .doc and the newer .docx (which is actually a .zip file > with a particular content-structure renamed to .docx). > I am using .doc files only.. > > F

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread scottcabit
> > re.sub _returns_ its result (strings are immutable). Ahhso I tried this for each re.sub fStr = re.sub(b'‒','-',fStr) No errors running it, but it still does nothing. -- https://mail.python.org/mailman/listinfo/python-list

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread Tim Chase
Obviously a syntax > problemwwhat silly thing am I doing wrong? > > fn = 'z:\Documentation\Software' > def processdoc(fn,outfile): > fStr = open(fn, 'rb').read() > re.sub(b'‒','-',fStr) > re.sub(b'–','-&#

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread Chris Angelico
On Sat, May 10, 2014 at 5:51 AM, wrote: > But the replaces are not having any effect. Obviously a syntax > problemwwhat silly thing am I doing wrong? > > Thanks! > > fn = 'z:\Documentation\Software' > def processdoc(fn,outfile): > fStr = op

Re: Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread MRAB
. Obviously a syntax problemwwhat silly thing am I doing wrong? Thanks! fn = 'z:\Documentation\Software' def processdoc(fn,outfile): fStr = open(fn, 'rb').read() re.sub(b'‒','-',fStr) re.sub(b'–','-',fStr) re

Why isn't my re.sub replacing the contents of my MS Word file?

2014-05-09 Thread scottcabit
wrong? Thanks! fn = 'z:\Documentation\Software' def processdoc(fn,outfile): fStr = open(fn, 'rb').read() re.sub(b'‒','-',fStr) re.sub(b'–','-',fStr) re.sub(b'—','-',fStr) re.sub(b'―&#x

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-19 Thread Ian Kelly
On Mon, Dec 19, 2011 at 4:15 PM, wrote: > On Dec 16, 11:49 am, John Gordon wrote: >> I'm working with IPv6 CIDR strings, and I want to replace the longest >> match of "(:|$)+" with ":".  But when I use re.sub() it replaces >> the leftmost m

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-19 Thread ting
On Dec 16, 11:49 am, John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(:|$)+" with ":".  But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the strin

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-19 Thread Duncan Booth
27; >> for word in re.findall('((:?)+)', ip6): >> if len(word[0])> len(longest_match): >> longest_match = word[0] >> >> # if we found a match, replace it with a colon >> if longest_match: >>ip6 = re.sub(longest_match, '

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread Terry Reedy
On 12/16/2011 1:36 PM, Roy Smith wrote: What you want is an IPv6 class which represents an address in some canonical form. It would have constructors which accept any of the RFC-2373 defined formats. It would also have string formatting methods to convert the internal form into any of these fo

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread MRAB
match, replace it with a colon if longest_match: ip6 = re.sub(longest_match, ':', ip6, 1) For a simple replace, using re is probably overkill. The .replace method is a better solution: ip6 = longest_match.replace(ip6, ':', 1) -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread John Gordon
In Roy Smith writes: > Having done quite a bit of IPv6 work, my opinion here is that you're > trying to do The Wrong Thing. > What you want is an IPv6 class which represents an address in some > canonical form. It would have constructors which accept any of the > RFC-2373 defined formats.

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread John Gordon
In Ian Kelly writes: > >>> I'm also looking for a regexp that will remove leading zeroes in each > >>> four-digit group, but will leave a single zero if the group was all > >>> zeroes. > pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b'

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread John Gordon
lon if longest_match: ip6 = re.sub(longest_match, ':', ip6, 1) Thanks! -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Ti

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread Roy Smith
In article , John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(:|$)+" with ":". But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. >

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread MRAB
On 16/12/2011 17:57, Ian Kelly wrote: On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: On 16/12/2011 16:49, John Gordon wrote: According to the documentation on re.sub(), it replaces the leftmost matching pattern. However, I want to replace the *longest* matching pattern, which is not

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread Ian Kelly
On Fri, Dec 16, 2011 at 10:57 AM, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>>

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread Ian Kelly
On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: > On 16/12/2011 16:49, John Gordon wrote: >> >> According to the documentation on re.sub(), it replaces the leftmost >> matching pattern. >> >> However, I want to replace the *longest* matching pattern, which is >

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread MRAB
On 16/12/2011 16:49, John Gordon wrote: According to the documentation on re.sub(), it replaces the leftmost matching pattern. However, I want to replace the *longest* matching pattern, which is not necessarily the leftmost match. Any suggestions? I'm working with IPv6 CIDR strings,

Re: re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread Devin Jeanpierre
it's slice.start and slice.stop, but match.start() and match.end() ? On Fri, Dec 16, 2011 at 11:49 AM, John Gordon wrote: > According to the documentation on re.sub(), it replaces the leftmost > matching pattern. > > However, I want to replace the *longest* matching patter

re.sub(): replace longest match instead of leftmost match?

2011-12-16 Thread John Gordon
According to the documentation on re.sub(), it replaces the leftmost matching pattern. However, I want to replace the *longest* matching pattern, which is not necessarily the leftmost match. Any suggestions? I'm working with IPv6 CIDR strings, and I want to replace the longest match of &

Re: re.sub: escaping capture group followed by numeric(s)

2010-09-17 Thread Jon Clements
On 17 Sep, 19:59, Peter Otten <__pete...@web.de> wrote: > Jon Clements wrote: > > (I reckon this is probably a question for MRAB and is not really > > Python specific, but anyhow...) > > > Absolutely basic example: re.sub(r'(\d+)', r'\1', '

Re: re.sub: escaping capture group followed by numeric(s)

2010-09-17 Thread Peter Otten
Jon Clements wrote: > (I reckon this is probably a question for MRAB and is not really > Python specific, but anyhow...) > > Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1') > > I've been searching around and I'm sure it&#

Re: re.sub: escaping capture group followed by numeric(s)

2010-09-17 Thread MRAB
On 17/09/2010 19:21, Jon Clements wrote: Hi All, (I reckon this is probably a question for MRAB and is not really Python specific, but anyhow...) Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1') I've been searching around and I'm sure it&

re.sub: escaping capture group followed by numeric(s)

2010-09-17 Thread Jon Clements
Hi All, (I reckon this is probably a question for MRAB and is not really Python specific, but anyhow...) Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1') I've been searching around and I'm sure it'll be obvious when it's pointed ou

Re: Using re.sub with %s

2010-08-18 Thread MRAB
Thomas Jollans wrote: On Wednesday 18 August 2010, it occurred to Brandon Harris to exclaim: Having trouble using %s with re.sub test = '/my/word/whats/wrong' re.sub('(/)word(/)', r'\1\%s\2'%'1000', test) return is /my/@0/whats/wrong This has n

Re: Using re.sub with %s

2010-08-18 Thread Thomas Jollans
On Wednesday 18 August 2010, it occurred to Brandon Harris to exclaim: > Having trouble using %s with re.sub > > test = '/my/word/whats/wrong' > re.sub('(/)word(/)', r'\1\%s\2'%'1000', test) > > return is /my/@0/whats/wrong > T

Using re.sub with %s

2010-08-18 Thread Brandon Harris
Having trouble using %s with re.sub test = '/my/word/whats/wrong' re.sub('(/)word(/)', r'\1\%s\2'%'1000', test) return is /my/@0/whats/wrong however if I cast a value with letters as opposed to numbers re.sub('(/)word(/)', r'\1\%s\2&#x

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-16 Thread Steven D'Aprano
gt;> >> You're passing re.IGNORECASE (which happens to equal 2) as a count >> >> argument, not as a flag. Try this instead: >> >> >> >>> re.sub(r"python\d\d" + '(?i)', "Python27", t) >> >> 'Python27

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-16 Thread Alex Willmer
On Aug 16, 1:46 pm, Alex Willmer wrote: > "Note that the (?x) flag changes how the expression is parsed. It > should be used first in the expression string, or after one or more > whitespace characters. If there are non-whitespace characters before > the flag, the results are undefined. > "http://

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-16 Thread Alex Willmer
t;> argument, not as a flag. Try this instead: > > >> >>> re.sub(r"python\d\d" + '(?i)', "Python27", t) > >> 'Python27' > > > Basically right, but in-line flags must be placed at the start of a > > pattern, or the

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-16 Thread Christopher
On Aug 15, 8:07 pm, Steven D'Aprano wrote: > On Sun, 15 Aug 2010 16:45:49 -0700, Christopher wrote: > > I have the following problem: > > >>>> t="Python26" > >>>> import re > >>>> re.sub(r"python\d\d", "

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-16 Thread Steven D'Aprano
On Sun, 15 Aug 2010 17:36:07 -0700, Alex Willmer wrote: > On Aug 16, 1:07 am, Steven D'Aprano cybersource.com.au> wrote: >> You're passing re.IGNORECASE (which happens to equal 2) as a count >> argument, not as a flag. Try this instead: >> >> >>

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-15 Thread MRAB
Alex Willmer wrote: On Aug 16, 1:07 am, Steven D'Aprano wrote: You're passing re.IGNORECASE (which happens to equal 2) as a count argument, not as a flag. Try this instead: re.sub(r"python\d\d" + '(?i)', "Python27", t) 'Python27' Basica

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-15 Thread Alex Willmer
On Aug 16, 1:07 am, Steven D'Aprano wrote: > You're passing re.IGNORECASE (which happens to equal 2) as a count > argument, not as a flag. Try this instead: > > >>> re.sub(r"python\d\d" + '(?i)', "Python27", t) > 'Python27&

Re: Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-15 Thread Steven D'Aprano
On Sun, 15 Aug 2010 16:45:49 -0700, Christopher wrote: > I have the following problem: > >>>> t="Python26" >>>> import re >>>> re.sub(r"python\d\d", "Python27", t) > 'Python26' >>>> re.sub(r

Python 2.7 re.IGNORECASE broken in re.sub?

2010-08-15 Thread Christopher
I have the following problem: Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> t="Python26" >>> import re >>

Re: re.sub and variables

2010-08-12 Thread MRAB
ot; % {"var": t} "Hello wSPAMd".replace("SPAM", t) or many other variations. Constructing a regex is no different. You just need to remember that if you pass a string into re.sub as a replacement then it'll be treated as a template. It's all in the documentation! :-) -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub and variables

2010-08-12 Thread Steven D'Aprano
On Thu, 12 Aug 2010 14:33:28 -0700, fuglyducky wrote: > if anyone happens to know about > passing a variable into a regex that would be great. The same way you pass anything into any string. Regexes are ordinary strings. If you want to construct a string from a variable t = "orl", you can do an

Re: re.sub and variables

2010-08-12 Thread John Machin
On Aug 13, 7:33 am, fuglyducky wrote: > On Aug 12, 2:06 pm, fuglyducky wrote: > > > > > I have a function that I am attempting to call from another file. I am > > attempting to replace a string using re.sub with another string. The > > problem is that the second st

Re: re.sub and variables

2010-08-12 Thread fuglyducky
On Aug 12, 2:06 pm, fuglyducky wrote: > I have a function that I am attempting to call from another file. I am > attempting to replace a string using re.sub with another string. The > problem is that the second string is a variable. When I get the > output, it shows the variable name

re.sub and variables

2010-08-12 Thread fuglyducky
I have a function that I am attempting to call from another file. I am attempting to replace a string using re.sub with another string. The problem is that the second string is a variable. When I get the output, it shows the variable name rather than the value. Is there any way to pass a variable

Re: re.sub unexpected behaviour

2010-07-06 Thread Javier Collado
Thanks for your answers. They helped me to realize that I was mistakenly using match.string (the whole string) when I should be using math.group(0) (the whole match). Best regards, Javier -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub unexpected behaviour

2010-07-06 Thread Steven D'Aprano
On Tue, 06 Jul 2010 19:10:17 +0200, Javier Collado wrote: > Hello, > > Let's imagine that we have a simple function that generates a > replacement for a regular expression: > > def process(match): > return match.string > > If we use that simple function wi

Re: re.sub unexpected behaviour

2010-07-06 Thread Thomas Jollans
On 07/06/2010 07:10 PM, Javier Collado wrote: > Hello, > > Let's imagine that we have a simple function that generates a > replacement for a regular expression: > > def process(match): > return match.string > > If we use that simple function with re.sub using

re.sub unexpected behaviour

2010-07-06 Thread Javier Collado
Hello, Let's imagine that we have a simple function that generates a replacement for a regular expression: def process(match): return match.string If we use that simple function with re.sub using a simple pattern and a string we get the expected output: re.sub('123', proce

Re: Bug? concatenate a number to a backreference: re.sub(r'(zzz:)xxx', r'\1'+str(4444), somevar)

2009-10-23 Thread abdulet
rocess > > logic but what a sorprise!!! when arrived to this conclussion after > > some time debugging i see that: > > > import re > > aa = "zzz:xxx" > > re.sub(r'(zzz:).*',r'\1'+str(),aa) > > '[33' > >

Re: Bug? concatenate a number to a backreference: re.sub(r'(zzz:)xxx', r'\1'+str(4444), somevar)

2009-10-23 Thread Peter Otten
> some time debugging i see that: > > import re > aa = "zzz:xxx" > re.sub(r'(zzz:).*',r'\1'+str(),aa) > '[33' If you perform the addition you get r"\1". How should the regular expression engine interpret that? As the back

Bug? concatenate a number to a backreference: re.sub(r'(zzz:)xxx', r'\1'+str(4444), somevar)

2009-10-23 Thread abdulet
: import re aa = "zzz:xxx" re.sub(r'(zzz:).*',r'\1'+str(),aa) '[33' ¿?¿?¿? well lets put a : after the backreference aa = "zzz:xxx" re.sub(r'(zzz).*',r'\1:'+str(),aa) 'zzz:' now its the expected result s

Re: re.sub question (regular expressions)

2009-10-20 Thread Chris Seberino
On Oct 16, 9:51 am, MRAB wrote: > What do you mean "blow up"? It worked for me in Python v2.6.2. My bad. False alarm. This was one of those cases where a bug in another area appears like a bug in a different area. Thank for the help. cs -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub question (regular expressions)

2009-10-16 Thread MRAB
Chris Seberino wrote: What does this line do?... input_ = re.sub("([a-zA-Z]+)", '"\\1"', input_) Why don't you try it? Does it remove parentheses from words? e.g. (foo) -> foo ??? No, it puts quotes around them. I'd like to replace [a-zA-Z]

Re: re.sub question (regular expressions)

2009-10-16 Thread Jean-Michel Pichavant
Chris Seberino wrote: What does this line do?... input_ = re.sub("([a-zA-Z]+)", '"\\1"', input_) Does it remove parentheses from words? e.g. (foo) -> foo ??? I'd like to replace [a-zA-Z] with \w but \w makes it blow up. In other words, re.sub("(

re.sub question (regular expressions)

2009-10-16 Thread Chris Seberino
What does this line do?... input_ = re.sub("([a-zA-Z]+)", '"\\1"', input_) Does it remove parentheses from words? e.g. (foo) -> foo ??? I'd like to replace [a-zA-Z] with \w but \w makes it blow up. In other words, re.sub("(\w+)", '"\\1

re.sub do not replace portion of match

2009-10-03 Thread J Wolfe
Hi, Is there a way to flag re.sub not to replace a portion of the string? I have a very long string that I want to add two new line's to rather than one, but keep the value X: string = "testX.\n.today" <-- note X is a value string = re.sub("testX.\

Re: re.sub do not replace portion of match

2009-10-03 Thread Duncan Booth
J Wolfe wrote: > Hi, > > Is there a way to flag re.sub not to replace a portion of the string? > > I have a very long string that I want to add two new line's to rather > than one, but keep the value X: > > string = "testX.\n.today" <--

Re: re.sub do not replace portion of match

2009-10-02 Thread J Wolfe
Thanks Duncan, I did look at that, but it was kinda greek to me. Thanks for pulling out the part I was looking for that should do the trick. Jonathan > http://www.python.org/doc/current/library/re.html#re.sub > > > Backreferences, such as \6, are replaced with the substrin

Re: How to pass multiline flag to re.sub without using re.complie.

2009-05-23 Thread Tim Chase
I have a regex that needs multiline flag. Some where I read I can pass multiline flag in regex string itself without using re.compile. If anybody have any idea about how to do that please reply. As detailed at [1], """ (?iLmsux) (One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.)

Re: How to pass multiline flag to re.sub without using re.complie.

2009-05-23 Thread MRAB
samba wrote: I have a regex that needs multiline flag. Some where I read I can pass multiline flag in regex string itself without using re.compile. If anybody have any idea about how to do that please reply. Include "(?m)" in the regular expression for multiline matching. It's best to put it a

How to pass multiline flag to re.sub without using re.complie.

2009-05-23 Thread samba
I have a regex that needs multiline flag. Some where I read I can pass multiline flag in regex string itself without using re.compile. If anybody have any idea about how to do that please reply. -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub and named groups

2009-02-11 Thread Rhodri James
On Wed, 11 Feb 2009 21:05:53 -, Paul McGuire wrote: On Feb 4, 10:51 am, "Emanuele D'Arrigo" wrote: Hi everybody, I'm having a ball with the power of regular expression Don't forget the ball you can have with the power of ordinary Python strings, string methods, and string interpolati

Re: re.sub and named groups

2009-02-11 Thread Paul McGuire
On Feb 4, 10:51 am, "Emanuele D'Arrigo" wrote: > Hi everybody, > > I'm having a ball with the power of regular expression Don't forget the ball you can have with the power of ordinary Python strings, string methods, and string interpolation! originalString = "spam:%(first)s ham:%(second)s" print

Re: re.sub and named groups

2009-02-11 Thread Shawn Milochik
> > Book recommendation: _Mastering Regular Expressions_, Jeffrey Friedl > -- > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ I wholeheartedly second this! The third edition is out now. -- http://mail.python.org/mailman/listinfo/python-list

Re: re.sub and named groups

2009-02-10 Thread Aahz
In article <4c7158d2-5663-46b9-b950-be81bd799...@z6g2000pre.googlegroups.com>, Emanuele D'Arrigo wrote: > >I'm having a ball with the power of regular expression but I stumbled >on something I don't quite understand: Book recommendation: _Mastering Regular Expressions_, Jeffrey Friedl -- Aahz (a

Re: re.sub and named groups

2009-02-04 Thread Yapo Sébastien
> Hi everybody, > > I'm having a ball with the power of regular expression but I stumbled > on something I don't quite understand: > > theOriginalString = "spam:(?P.*) ham:(?P.*)" > aReplacementPattern = "\(\?P.*\)" > aReplacementString= &

Re: re.sub and named groups

2009-02-04 Thread Emanuele D'Arrigo
On Feb 4, 5:17 pm, MRAB wrote: > You could use the lazy form "*?" which tries to match as little as > possible, eg "\(\?P.*?\)" where the ".*?" matches: > spam:(?P.*) ham:(?P.*) > giving "spam:foo ham:(?P.*)". A-ha! Of course! That makes perfect sense! Thank you! Problem solved! Ciao! Manu --

Re: re.sub and named groups

2009-02-04 Thread MRAB
Emanuele D'Arrigo wrote: > Hi everybody, > > I'm having a ball with the power of regular expression but I stumbled > on something I don't quite understand: > > theOriginalString = "spam:(?P.*) ham:(?P.*)" > aReplacementPattern = "\(\?P.*\)"

re.sub and named groups

2009-02-04 Thread Emanuele D'Arrigo
Hi everybody, I'm having a ball with the power of regular expression but I stumbled on something I don't quite understand: theOriginalString = "spam:(?P.*) ham:(?P.*)" aReplacementPattern = "\(\?P.*\)" aReplacementString= "foo" re.sub(aReplacementPattern

Re: small problem with re.sub

2008-01-30 Thread Gabriel Genellina
En Thu, 31 Jan 2008 01:01:30 -0200, Astan Chee <[EMAIL PROTECTED]> escribió: > I have a html text stored as a string. Now I want to go through this > string and find all 6 digit numbers and make links from them. > Im using re.sub and for some reason its not picking up the previ

  1   2   >