Re: String Replace only if whole word?
Michael Yanowitz schrieb: Yeah, I am operating this on a Python script. However, I am working off a requirement that the script be pre-processed and the strings replaced before executing the script and that if there are any remaining (not replaced) names that I don't execute the script and report that some 'mnemonics' have not been replaced. If you control the source(template), you could use string formatting. http://docs.python.org/lib/typesseq-strings.html You would have to rewrite your MNEMONIC as %(MNEMONIC)s . An exception will be raised if you miss a replacement and you are sure you do not replace anything by accident. Leonhard -- http://mail.python.org/mailman/listinfo/python-list
Re: String Replace only if whole word?
Michael Yanowitz wrote: Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100 172.18.50.138 SIB 172.18.50.138 LAPTOP 172.18.51.32 WIN2000 127.0.0.1 LOCALHOST and I have a text file (a Python script) that has these names in the file. In most cases the string.replace() command works great. But there is one instance which it fails: Suppose I had in the file: if (LAPTOP_IS_UP()): It would replace the string with: if (172.18.50.138_IS_UP()): Is there any easy way to avoid this, only replace if a whole word matches? I probably need something which determines when a word ends, and I will define a word as containing only 'A'-'Z','a'-'z','0'-'9','_' . As long as the string contains more of the word digits after the match, don't replace? Thanks in advance: Michael Yanowitz You need regular expressions for this. Use the re module. http://docs.python.org/lib/module-re.html from the docs: re.sub(pattern, repl, string[, count]) Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. Your pattern would be [^A-Za-z0-9_]word[^A-Za-z0-9_] [^xy] is approximately not in ('x', 'y') -- Juho Schultz -- http://mail.python.org/mailman/listinfo/python-list
RE: String Replace only if whole word?
Michael Yanowitz wrote: Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100 172.18.50.138 SIB 172.18.50.138 LAPTOP 172.18.51.32 WIN2000 127.0.0.1 LOCALHOST and I have a text file (a Python script) that has these names in the file. In most cases the string.replace() command works great. But there is one instance which it fails: Suppose I had in the file: if (LAPTOP_IS_UP()): It would replace the string with: if (172.18.50.138_IS_UP()): Is there any easy way to avoid this, only replace if a whole word matches? I probably need something which determines when a word ends, and I will define a word as containing only 'A'-'Z','a'-'z','0'-'9','_' . As long as the string contains more of the word digits after the match, don't replace? Thanks in advance: Michael Yanowitz You need regular expressions for this. Use the re module. http://docs.python.org/lib/module-re.html from the docs: re.sub(pattern, repl, string[, count]) Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. Your pattern would be [^A-Za-z0-9_]word[^A-Za-z0-9_] [^xy] is approximately not in ('x', 'y') -- Juho Schultz Thanks. This works great except for one thing: The character after the replacement is deleted, so that if I have send_data (LAPTOP, test_string) it would replace it with: send_data (10.1.3.4 test_string) (ignoring that the 10.1.3.4 is not quoted). The comma is missing. Thanks in advance: Michael Yanowitz -- http://mail.python.org/mailman/listinfo/python-list
RE: String Replace only if whole word?
On Fri, 2006-11-17 at 10:43 -0500, Michael Yanowitz wrote: Your pattern would be [^A-Za-z0-9_]word[^A-Za-z0-9_] -- Juho Schultz Thanks. This works great except for one thing: The character after the replacement is deleted, so that if I have send_data (LAPTOP, test_string) it would replace it with: send_data (10.1.3.4 test_string) (ignoring that the 10.1.3.4 is not quoted). The comma is missing. If you actually wanted to use string replacement, you'd need a pattern with look-behind and look-ahead assertions along the lines of (?![A-Za-z0-9_])word(?![A-Za-z0-9_]). Then again, I don't think string replacement is the right tool for the job. You're saying that the file you are operating on in this fashion is a Python script. Why don't you just place something like LAPTOP=10.1.3.4 at the top of that file, or have the script read this information from a configuration file? -Carsten -- http://mail.python.org/mailman/listinfo/python-list
Re: String Replace only if whole word?
I have been using the string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100 172.18.50.138 SIB 172.18.50.138 LAPTOP 172.18.51.32 WIN2000 127.0.0.1 LOCALHOST and I have a text file (a Python script) that has these names in the file. In most cases the string.replace() command works great. But there is one instance which it fails: Suppose I had in the file: if (LAPTOP_IS_UP()): It would replace the string with: if (172.18.50.138_IS_UP()): Is there any easy way to avoid this, only replace if a whole word matches? I probably need something which determines when a word ends, and I will define a word as containing only 'A'-'Z','a'-'z','0'-'9','_' . As long as the string contains more of the word digits after the match, don't replace? A solution I've used in the past (whether it's good or not, I'd be interested to get feedback from the list) mapping = {} for line in file('definitions.txt'): ... ip, name = line.split() ... mapping[name] = '%s' % ip ... import re s = LAPTOP LAPTOP_IS_UP r = re.compile(r'\b\w+\b') r.sub(lambda match: mapping.get(match.group(0), match.group(0)), s) '172.18.50.138 LAPTOP_IS_UP' The regexp '\b\w+\b' finds words (where \w is locale specific, but could easily be rewritten as '\b[a-zA-Z0-9_]+\b' if you needed) while the '\b' portion ensures that there's a word-to-non-word boundary coming or going. It then goes through and replaces every word in the string with either the result of looking it up in the magic mapping, or with its original contents. -tkc -- http://mail.python.org/mailman/listinfo/python-list
RE: String Replace only if whole word?
-Original Message- From: Carsten Haese [mailto:[EMAIL PROTECTED] Sent: Friday, November 17, 2006 11:03 AM To: Michael Yanowitz Cc: python-list@python.org Subject: RE: String Replace only if whole word? On Fri, 2006-11-17 at 10:43 -0500, Michael Yanowitz wrote: Your pattern would be [^A-Za-z0-9_]word[^A-Za-z0-9_] -- Juho Schultz Thanks. This works great except for one thing: The character after the replacement is deleted, so that if I have send_data (LAPTOP, test_string) it would replace it with: send_data (10.1.3.4 test_string) (ignoring that the 10.1.3.4 is not quoted). The comma is missing. If you actually wanted to use string replacement, you'd need a pattern with look-behind and look-ahead assertions along the lines of (?![A-Za-z0-9_])word(?![A-Za-z0-9_]). Then again, I don't think string replacement is the right tool for the job. You're saying that the file you are operating on in this fashion is a Python script. Why don't you just place something like LAPTOP=10.1.3.4 at the top of that file, or have the script read this information from a configuration file? -Carsten Thanks, works great (so far). Yeah, I am operating this on a Python script. However, I am working off a requirement that the script be pre-processed and the strings replaced before executing the script and that if there are any remaining (not replaced) names that I don't execute the script and report that some 'mnemonics' have not been replaced. Thanks: Michael Yanowitz -- http://mail.python.org/mailman/listinfo/python-list