regex question

F

Fred Mangusta

Hi,

I would like to delete all the instances of a '.' into a number.

In other words I'd like to replace all the instances of a '.' character
with something (say nothing at all) when the '.' is representing a
decimal separator. E.g.

500.675 ----> 500675

but also

1.000.456.344 ----> 1000456344

I don't care about the fact the the resulting number is difficult to
read: as long as it remains a series of digits it's ok: the important
thing is to get rid of the period, because I want to keep it only where
it marks the end of a sentence.

I was trying to do like this

s=re.sub("[(\d+)(\.)(\d+)]","... ",s)

but I don't know much about regular expressions, and don't know how to
get the two groups of numbers and join them in the sub. Moreover doing
like this I only match things like "345.000" and not "1.000.000".

What's the correct approach?

Thanks
F.
 
M

Marc 'BlackJack' Rintsch

In other words I'd like to replace all the instances of a '.' character
with something (say nothing at all) when the '.' is representing a
decimal separator. E.g.

500.675 ----> 500675

but also

1.000.456.344 ----> 1000456344

I don't care about the fact the the resulting number is difficult to
read: as long as it remains a series of digits it's ok: the important
thing is to get rid of the period, because I want to keep it only where
it marks the end of a sentence.

I was trying to do like this

s=re.sub("[(\d+)(\.)(\d+)]","... ",s)

but I don't know much about regular expressions, and don't know how to
get the two groups of numbers and join them in the sub. Moreover doing
like this I only match things like "345.000" and not "1.000.000".

What's the correct approach?

In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344')
Out[13]: '1000456344'

Ciao,
Marc 'BlackJack' Rintsch
 
J

Jeff

In other words I'd like to replace all the instances of a '.' character
with something (say nothing at all) when the '.' is representing a
decimal separator. E.g.
500.675  ---->       500675
1.000.456.344 ----> 1000456344
I don't care about the fact the the resulting number is difficult to
read: as long as it remains a series of digits it's ok: the important
thing is to get rid of the period, because I want to keep it only where
it marks the end of a sentence.
I was trying to do like this
s=re.sub("[(\d+)(\.)(\d+)]","... ",s)
but I don't know much about regular expressions, and don't know how to
get the two groups of numbers and join them in the sub. Moreover doing
like this I only match things like "345.000" and not "1.000.000".
What's the correct approach?

In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344')
Out[13]: '1000456344'

Ciao,
        Marc 'BlackJack' Rintsch

Even faster:

'1.000.456.344'.replace('.', '') => '1000456344'
 
C

Chris

In other words I'd like to replace all the instances of a '.' character
with something (say nothing at all) when the '.' is representing a
decimal separator. E.g.
500.675  ---->       500675
but also
1.000.456.344 ----> 1000456344
I don't care about the fact the the resulting number is difficult to
read: as long as it remains a series of digits it's ok: the important
thing is to get rid of the period, because I want to keep it only where
it marks the end of a sentence.
I was trying to do like this
s=re.sub("[(\d+)(\.)(\d+)]","... ",s)
but I don't know much about regular expressions, and don't know how to
get the two groups of numbers and join them in the sub. Moreover doing
like this I only match things like "345.000" and not "1.000.000".
What's the correct approach?
In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344')
Out[13]: '1000456344'
Ciao,
        Marc 'BlackJack' Rintsch

Even faster:

'1.000.456.344'.replace('.', '') => '1000456344'

Doesn't work for his use case as he wants to keep periods marking the
end of a sentence.
 
F

Fred Mangusta

Chris said:
Doesn't work for his use case as he wants to keep periods marking the
end of a sentence.

Exactly. Thanks to all of you anyway, now I have a better understanding
on how to go on :)

F.
 
M

MRAB

Hi,

I would like to delete all the instances of a '.' into a number.

In other words I'd like to replace all the instances of a '.' character
with something (say nothing at all) when the '.' is representing a
decimal separator. E.g.

500.675  ---->       500675

but also

1.000.456.344 ----> 1000456344

I don't care about the fact the the resulting number is difficult to
read: as long as it remains a series of digits it's ok: the important
thing is to get rid of the period, because I want to keep it only where
it marks the end of a sentence.

I was trying to do like this

s=re.sub("[(\d+)(\.)(\d+)]","... ",s)

but I don't know much about regular expressions, and don't know how to
get the two groups of numbers and join them in the sub. Moreover doing
like this I only match things like "345.000" and not "1.000.000".

What's the correct approach?
I would use look-behind (is it preceded by a digit?) and look-ahead
(is it followed by a digit?):

s = re.sub(r'(?<=\d)\.(?=\d)', '', s)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RegEx 0
Question 0
Replace various regex 8
regex question 2
SQL Connection string regex pattern to parse sections 1
Math python question 10
Creating a regex to get multiple values and print 0
Shoelace Formula 3

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top