how to find match first word in string with regex in python
<p>I want to match the word <code>'St' or 'St.' or 'st' or 'st.'</code> BUT only in first word of a string.
For example ' St. Mary Church Church St.' - should find ONLY first St. </p>

<ul>
<li>'st. Mary Church Church St.' - should find ONLY 'st.' </li>
<li>'st Mary Church Church St.' - should find ONLY 'st'</li>
</ul>

<p>I want to eventually replace the first occurence with 'Saint'.</p>

<p>I have literally spent hours trying to find a regex that will match this problem so i have tried myself first and i now for some of you it will be easy!</p>

<p>You don't need to use a regex for this, just use the <code>split()</code> method on your string to split it by whitespace. This will return a list of every word in your string:</p>

<pre><code>matches = ["St", "St.", "st", "st."]
name = "St. Mary Church Church St."
words = name.split()   #split the string into words into a list
if words [0] in matches:
    words[0] = "Saint"   #replace the first word in the list (St.) with Saint
new_name = "".join([word + " " for word in words]).strip()   #create the new name from the words, separated by spaces and remove the last whitespace
print(new_name)   #Output: "Saint Mary Church Church St."
</code></pre>

python regex