Regular expression to replace all occurrences of U.S
<p>I'm trying to write a regular expression to replace all occurrences of U.S. . Here's what I thought would work.</p>

<pre><code>string = re.sub(r'\bU.S.\b', 'U S ', string)
</code></pre>

<p>When I run this it only finds the first occurrence. Why is this and how can I resolve this issue. Thanks </p>

<p>if you are searching in a file, to find all occurrences and replace them, you need to search line by line.</p>

<p>the . need to be \. because . itself has other meanings in RE.
A safer way to implement is to write \b+ so it counts for 1 or more such cases.</p>

<p>r doesnt mean repeat, it means escape character wont be processed</p>

<p>by the way, you dont need to worry \b after the . because the RE will match everything before that, and ignore what is not matching. You are indeed getting the first part printed first time while you have the wrong RE, right?</p>

python