Pergunta

I would like a regex which will split a string at every "." except if the "." is preceded AND followed by an number. example:

"hello world.foo 1.1 bar.1" ==> ["hello world","foo 1.1 bar", "1"]

I currently have:

"(?<![0-9])\.(?!\d)" 

but it gives:

["hello world", "foo 1.1 bar.1"]

but its not finding the last "." valid.

Foi útil?

Solução 2

Split on . if it is not preceded by a digit, or if it is not succeeded by a digit:

In [18]: re.split(r'(?<!\d)\.|\.(?!\d)', text)
Out[18]: ['hello world', 'foo 1.1 bar', '1']

Outras dicas

A non-| approach:

(?<![0-9](?=.[0-9]))\.

That's because only one of those assertions have to fail for the whole expression to fail. Try this:

"(?<![0-9])\.|\.(?!\d)"

Just for the sake of contributing the shortest solution, here is mine:

(it is simply @ysth's solution with a small adjustment)

(?<!\d(?=.\d))\.

working fiddle

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top