Reputation: 13
i want to ask how we can replace only the first word in regular expression in python i do this code :
print "number of all KSU occurrences %i "%len(re.findall("ksu",string,re.IGNORECASE))
print "number of all KSU at end lof line %i"%len(re.findall("ksu\n",string,re.IGNORECASE))
and this is my txt file
hello i'm student in KSU i love ksu , i like ksu , i KSU ksu KSU
i just want to replace only first ksu ( ignoring case ) into King Saud University
Upvotes: 1
Views: 131
Reputation: 336078
re.sub()
takes a count
parameter:
re.sub(r"\bksu\b", "King Saud University", string, flags=re.I, count=1)
Upvotes: 1