# replace « regex « Python Data Type Q&A

Home
Python Data Type Q&A
 1 array 2 date 3 dictionary 4 Format 5 integer 6 List 7 numpy 8 regex 9 string 10 tuple
 Python Data Type Q&A » regex » replace

### 1. Regular expression: replace the suffix of a string ending in '.js' but not 'min.js'    stackoverflow.com

Assume infile is a variable holding the name of an input file, and similarly outfile for output file. If infile ends in .js, I'd like to replace with .min.js and ...

### 2. Regex replace (in Python) - a more simple way?    stackoverflow.com

Any time I want to replace a piece of text that is part of a larger piece of text, I always have to do something like:

"(?P<start>some_pattern)(?P<replace>foo)(?P<end>end)"

And then concatenate the start group ...

### 3. How do I replace all punctuation in my string with "" in Python?    stackoverflow.com

If my string was:

Business -- way's

I'd like to turn this into:
Business  ways

ie. replace NON abc/123 into ""

### 4. Regular expression to replace with XML node    stackoverflow.com

I'm using Python to write a regular expression for replacing parts of the string with a XML node. The source string looks like:

Hello
REPLACE(str1) this is to replace
REPLACE(str2) this is to replace

And the ...

### 5. Mass string replace in python?    stackoverflow.com

Say I have a string that looks like this:

str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"

You'll notice a lot of locations in the string where there is an ...

### 6. How to replace by regular expression to lowercase in python    stackoverflow.com

I want to search key words (keys would be dynamic) and replace them in a certain format. For example: these data

keys = ["cat", "dog", "mouse"]
text = "Cat dog cat cloud miracle DOG ...

### 7. How to replace only part of the match with python re.sub    stackoverflow.com

I need to match two cases by one reg expression and do replacement 'long.file.name.jpg' -> 'long.file.name_suff.jpg' 'long.file.name_a.jpg' -> 'long.file.name_suff.jpg' I'm trying to do the following

re.sub('(\_a)?\.[^\.]*$' , '_suff.',"long.file.name.jpg")  But this is cut the extension '.jpg' and ... ### 8. Replace ",**" with a linebreak using RegEx (or something else) stackoverflow.com I'm getting started with RegEx and I was wondering if anyone could help me craft a statement to convert coordinates as follows: 145.00694,-37.80421,9 145.00686,-37.80382,9 145.00595,-37.8035,16 145.00586,-37.80301,16  to 145.00694,-37.80421 145.00686,-37.80382 145.00595,-37.8035 145.00586,-37.80301  (Strip off the last comma and ... ### 9. Python Case Insensitive Replace without hurting re cache stackoverflow.com related question: http://stackoverflow.com/questions/919056/python-case-insensitive-replace What's the best way to do a case insensitive replace WITHOUT HURTING THE CACHE in the re module? I'm monitoring carefully the cache to make sure my favorite ... ### 10. python regex match and replace stackoverflow.com I need to find, process and remove (one by one) any substrings that match a rather long regex: # p is a compiled regex # s is a string while 1: ... ### 11. python: replacing regex with BNF or pyparsing stackoverflow.com I am parsing a relatively simple text, where each line describes a game unit. I have little knowledge of parsing techniques, so I used the following ad hoc solution: class Unit: ... ### 12. Python: Replace with regex stackoverflow.com I need to replace part of a string. I was looking through the Python documentation and found re.sub. import re s = '<textarea id="Foo"></textarea>' output = re.sub(r'<textarea.*>(.*)</textarea>', 'Bar', s) print output >>>'Bar'  I was expecting this to ... ### 13. How to use Python re module to replace \n with nothing in a single file stackoverflow.com Regards to all. I'm developing a Image compression system using Python Image Library. The basic workflow is: • Read all images of a certain directory with : find /opt/images -name *.jpg > /opt/rs/images.txt • Read this ... ### 14. Multiline regex replace stackoverflow.com I want to transform a text like: $$foo bar$$  to <% tex foo bar %>  and $\alpha$ to <% tex \alpha %>. For the single line replace, I did this: re.sub(r"\$(.*)\$", r"<% tex \1 %>", text)  ...and it works fine. Now, I added ... ### 15. what is the right regex for this? stackoverflow.com what is the regex for such a task? --> replace "[[...:" with "[[" That is to say, I want to replace *some text * inside [[...: with [[. The problem with my code ... ### 16. python regular expression replacing part of a matched string stackoverflow.com i got an string that might look like this "myFunc('element','node','elementVersion','ext',12,0,0)"  i'm currently checking for validity using, which works fine myFunc$$(.+?)\,(.+?)\,(.+?)\,(.+?)\,(.+?)\,(.+?)\,(.+?)$$  now i'd like to replace whatever string is at the 3rd parameter. unfortunately i cant just ... ### 17. Replace first occurence of string stackoverflow.com I have some sample string. How can I replace first occurence of this string in longer string with empty sign ? Tried this but it's not working as far as I ... ### 18. how to replace regular expression based on the match results (e.g. a special function)? stackoverflow.com i have a search expression with 1-2 groups. i need to substitute each result with something that depends on the result value. e.g. in the following string, replace each matched digit with it's ... ### 19. Regex Replace [*] stackoverflow.com I have a string as follows: something.something[0].somethingelse[21].blah  I want to replace all the [*] section with an empty string so that I end up with a string like this: something.something.somethingelse.blah  (I'm doing this in Python ... ### 20. How to replace pairs of tokens in a string? stackoverflow.com New to python, competent in a few languages, but can't see a 'snazzy' way of doing the following. I'm sure it's screaming out for a regex, but any solution I can ... ### 21. Replacing all but specific elements using regex in Python stackoverflow.com Using regular expressions in Python, I am trying to remove all XML-type elements in a string, except those containing QUOTE, eg <QUOTE>, </QUOTE> or <QUOTE A="B"> should remain, but others such ... ### 22. How to input a regex in string.replace in python? stackoverflow.com I need some help on declaring some regex. my inputs as as such this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. ... ### 23. Python: string.replace vs re.sub stackoverflow.com For python 2.5, 2.6, should I be using string.replace or re.sub for basic text replacements. In PHP, this was explicitly stated but I can't find a similar note for python. ### 24. Newbie python - replace in an array stackoverflow.com In a file, I have the following lines NetConn_msa[0].time=0.0 NetConn_msa[1].time=0.0 etc for 60 elements.  I need to write a script to change the time from 0.0 to 0.5. I started with st.replace("delay=0.0","delay=0.05") and ... ### 25. Regex to replace %variables% stackoverflow.com I've been yanking clumps of hair out for 30 minutes doing this one... I have a dictionary, like so: {'search': 'replace', 'foo': 'bar'}  And a string like this: Foo bar %foo% % ... ### 26. Replace 'nnn0n' with 'nnn1n' in Python using regular expressions stackoverflow.com I would like to replace a string in form 'nnn0n' with the string in form 'nnn1n' where n is any digit. What the easiest way to do that? So, far I ... ### 27. replace all "\" with "\\" python stackoverflow.com Does anyone know how replace all \ with \\ in python? Ive tried: re.sub('\','\\',string)  But it screws it up because of the escape sequence. does anyone know the awnser to my question? ### 28. re.sub(...) replacing leftmost occurrences? stackoverflow.com $ pydoc re.sub :

sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
...

### 29. Python regex replace to create smiley faces    stackoverflow.com

I'd like to create a regex string that would turn this text:

Hello this is a mighty fine day today

into
8===D 8==D 8D D 8====D 8==D 8=D 8===D

is this possible with a python ...

### 30. Replace Ctr+H copy row and change it     stackoverflow.com

I need some help. I have many rows in my text file with this :

invoke glVertex3f,-0.352848,0.081168,-0.123057

I want to copy each row before him and change glVertex3f with glColor3f. The result will be like ...

### 31. How to replace all occurrences of X between Y's?    stackoverflow.com

I'm not sure if this problem solvable with regular expressions (in Perl5 syntax), but here is self-explanatory example:

smth Y1 test X foo X Y2 bar X Y1 X X Y2
s/?/Z/g
smth Y1 ...

### 32. RegEx replace in Python    stackoverflow.com

I'm pretty new to regular expressions. I've worked out the string (?i)\$url$([0-9])$ to match what I'm looking for. I want a string like "this is$url[2] a string" to be passed ...

### 33. python regex for string replacement    stackoverflow.com

I want to replace parts of a string that contains the following words "$%word$%" I want to replace it with the value of a dictionary with the corresponding key equal to word. In ...

### 34. Python regex: how to replace each instance of an occurrence with a different value?    stackoverflow.com

Suppose I have this string: s = "blah blah blah" Using Python regex, how can I replace each instance of "blah" with a different value (e.g. I have a list of values v ...

### 35. how to replace uppercase with underscore?    stackoverflow.com

I'm new to Python and I am trying to replace all uppercase-letters within a word to underscores, for example:

ThisIsAGoodExample

should become
this_is_a_good_example

Any ideas/tips/links/tutorials on how to achieve this?

### 36. How do I replace commas in a string    stackoverflow.com

I want to remove all commas from a string using regular expressions, I want letters, periods and numbers to remain just commas

### 37. Regex replacement    stackoverflow.com

I have a string like this one:

{{foobar
| option1 = foo
| option2 = foo
| <!-- more options -->
| something = xxx
| thisoption = xxx
| <!-- more options -->
}}

I want it to be ...

### 38. python regular expression replace    stackoverflow.com

I'm trying to change a string that contains substrings such as

the</span></p>
<p><span class=font7>currency

to
the currency

At the line break is CRLF The words before and after the code change. I only want to replace ...

### 39. Implement python replace() function without using regexp    stackoverflow.com

I'm trying to rewrite the equivalent of the python replace() function without using regexp. Using this code, i've managed to get it to work with single chars, but not with more ...

### 40. Python Regex replace    stackoverflow.com

Hey I'm trying to figure out a regular expression to do the following. Here is my string

Place,08/09/2010,"15,531","2,909",650

I need to split this string by the comma's. Though due to the comma's used ...

### 41. Regex for replacing missing value period    bytes.com

Thanks all for your help. I must say that it makes sense to just use the built-in split-to-list feature of strings and check each element. It seems pretty safe. However, it takes a long time to process a large dataset and check each value to see if it is a period, in this case the census numbers for the state of ...