I have a paragraph of text and a list of substrings from that text which I want to replace with a different substring.  newstring = "foo" re_word = r'{0}'.format(word) text = re.sub(re_word, newstring, text)   I'm doing this with a number of different 'paragraphs' of text. For some paragraphs it works fine, but for others I get the following error from the final line:  sre_constants.error: missing ), unterminated subpattern at position 10   I suspect that the string I'm looking for contains a character that confuses Regex, ie. I need to escape it. How might I get around that?
22511
I want to use input from a user as a regex pattern for a search over some text.  It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word (s): regex engine will take the (s) as a group.  I want it to treat it like a string &quot;(s)&quot; .  I can run replace on user input and replace the ( with \( and the ) with \) but the problem is I will need to do replace for every possible regex symbol. Do you know some better way ?
92395
I want to split a command line like string in single string parameters. How look the regular expression for it. The problem are that the parameters can be quoted. For example like:  "param 1" param2 "param 3"  should result in:  param 1, param2, param 3
40363
I'm trying to replace each , in the current file by a new line:  :%s/,/\n/g    But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.  What should I do?  If you are curious, like me, check the question  as well.
3154
I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?   I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.
2047
I have a string which is like this:  this is "a test"   I'm trying to write something in Python to split it up by space while ignoring spaces within quotes. The result I'm looking for is:  ['this','is','a test']   PS. I know you are going to ask "what happens if there are quotes within the quotes, well, in my application, that will never happen.
5492
How can brackets be escaped in using string.Format? For example: String val = &quot;1,2,3&quot; String.Format(&quot; foo {{0}}&quot;, val);  This example doesn't throw an exception, but it outputs the string foo {0}. Is there a way to escape the brackets?
102991
I have a large text template which needs tokenized sections replaced by other text. The tokens look something like this: ##USERNAME##.  My first instinct is just to use String.Replace(), but is there a better, more efficient way or is Replace() already optimized for this?
85789
I'm trying to use this code to replace spaces with _, it works for the first space in the string but all the other instances of spaces remain unchanged. Anybody know why?  function updateKey() {     var key=$("#title").val();     key=key.replace(" ","_");     $("#url_key").val(key); }
28688
How can I replace Line Breaks within a string in C#?
262
I don't really understand regular expressions.  Can you explain them to me in an easy-to-follow manner?  If there are any online tools or books, could you also link to them?
81981
In python   re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",", stroke )    To split a number by triplets, e.g.:   echo 123456789 | python -c 'import sys;import re; print re.sub(r"(?&lt;=.)(?=(?:...)+$)", ",",  sys.stdin.read());'  123,456,789   How to do the same with bash/awk?
90073
I want to remove a character "[" from a file.  I tried   sed -i 's/[//g' 'filename'   however I get the following error      sed: -e expression #1, char 6: unterminated `s' command
1394
Replacing strings in files based on certain search criteria is a very common task. How can I    replace string foo with bar in all files in the current directory? do the same recursively for sub directories? replace only if the file name matches another string? replace only if the string is found in a certain context? replace if the string is on a certain line number? replace multiple strings with the same replacement replace multiple strings with different replacements
6296
What regex pattern would need I to pass to  java.lang.String.split()  to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
18468
I'm developing an algorithm to parse a number out of a series of short-ish strings. These strings are somewhat regular, but there's a few different general forms and several exceptions. I'm trying to build a set of regexes that will handle the various forms and exceptions; I'll apply them one after another to see if I get a match.   One of these forms goes something like this:  X (Y) Z   Where:   X is a number I want to capture.  Z is static, pre-defined text. it's basically how I determine whether this particular form is applicable or not. Y is a a string of unknown length and content, surrounded by parenthesis.    Also: Y is optional; it doesn't always appear in a string with Z and X. So, I want to be able to extract the numbers from all of these strings:   10 Z 20 (foo) Z 30 (bar) Z   Right now, I have a regex that will capture the first one:  ([0-9]+) +Z   My problem is that I don't know how to construct a regex that will match a series of characters if and only if they're enclosed in parenthesis. Can this be done in a single regex?
39933
How can I get the start and end positions of all matches using the re module? For example given the pattern r'[a-z]' and the string 'a1b2c3d4' I'd want to get the positions where it finds each letter. Ideally, I'd like to get the text of the match back too.
80932
I have a data stream that may contain \r, \n, \r\n, \n\r or any combination of them.  Is there a simple way to normalize the data to make all of them simply become \r\n pairs to make display more consistent?  So something that would yield this kind of translation table:  \r     --&gt; \r\n \n     --&gt; \r\n \n\n   --&gt; \r\n\r\n \n\r   --&gt; \r\n \r\n   --&gt; \r\n \r\n\n --&gt; \r\n\r\n