How can I use re
to write a regex in Python that finds the pattern:
dot "." followed directly by any char [a-zA-Z] (not space or digit)
and then add a space between the dot and the char?
i.e.
str="Thanks.Bob"
newsttr="Thanks. Bob"
Thanks in advance, Zvi
Use the str. ljust() method to add spaces to the end of a string, e.g. result = my_str. ljust(6, ' ') . The ljust method takes the total width of the string and a fill character and pads the end of the string to the specified width with the provided fill character.
The space character does not have any special meaning, it just means "match a space". RE = re. compile(' +') So for your case a='rasd\nsa sd' print(re.search(' +', a))
To match a literal dot in a raw Python string ( r"" or r'' ), you need to escape it, so r"\." Unless the regular expression is stored inside a regular python string, in which case you need to use a double \ ( \\ ) instead. So, all of these are equivalent: '\\.
Matches zero or more repetitions of the preceding regex. For example, a* matches zero or more 'a' characters. That means it would match an empty string, 'a' , 'aa' , 'aaa' , and so on.
re.sub(r'\.([a-zA-Z])', r'. \1', oldstr)
re.sub('(?<=\.)(?=[a-zA-Z])', ' ', str)
Try
re.sub(r"\.([a-zA-Z])", ". \\1", "Thanks.Bob")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With