Tengo un texto grande al que le faltan espacios después de algunos de los puntos. Sin embargo, el texto también contiene números decimales.
Esto es lo que tengo hasta ahora para solucionar el problema usando expresiones regulares (estoy usando python):
re.sub(r"(?!\d\.\d)(?!\. )\.", '. ', my_string)
Pero el primer grupo de escape no parece funcionar. Todavía coincide con períodos en números decimales.
Aquí hay un texto de muestra para asegurarse de que cualquier posible solución funcione:
this is a.match this should also match.1234 and this should 123.match this should NOT match. Has space after period this also should NOT match 1.23Puedes usar
re.sub(r'\.(?!(?<=\d\.)\d) ?', '. ', text)Vea la demostración de expresiones regulares . El espacio final se empareja opcionalmente, por lo que si está allí, se eliminará y se volverá a colocar.
Detalles
\. - un punto(?!(?<=\d\.)\d) - no coincidan más si el punto anterior era un punto entre dos dígitos? - un espacio opcional.Vea una demostración de Python :
import re text = "this is a.match\nthis should also match.1234\nand this should 123.match\n\nthis should NOT match. Has space after period\nthis also should NOT match 1.23" print(re.sub(r'\.(?!(?<=\d\.)\d) ?', '. ', text))Producción:
this is a. match this should also match. 1234 and this should 123. match this should NOT match. Has space after period this also should NOT match 1.23 Alternativamente, use una anticipación (?! ) como en su intento:
re.sub(r'\.(?!(?<=\d\.)\d)(?! )', '. ', text)Vea la demostración de expresiones regulares y la demostración de Python .
Otra forma... no estoy seguro de si esto es mejor o peor para el rendimiento que la solución de Wiktor.
re.sub(r"(?!\d\.\d)(?!.\. )(.\.)(.)", r"\1 \2", my_string)txt="hello world.this is boise idaho.a this is twin falls." pattern=r"(\w+\s*\.\w+)+" matches=re.findall(pattern,txt) for item in matches: front,back=item.split('.') replace=front+'. '+back txt=re.sub(item,replace,txt) print(txt) hello world. this is boise idaho. a this is twin falls.