Remove the Dots in the String

removing dot symbol from a string

Split the string on all the .'s and then join it again with empty spaces, like this:

checkedNew = checked.split('.').join("");

Remove dots and spaces from strings

You can use

s.replace(/(\d)[\s.]+(?=\d)/g, '$1')
s.replace(/(?<=\d)[\s.]+(?=\d)/g, '')

See the regex demo.

Details

  • (\d) - Group 1 ($1 in the replacement pattern is the value of the group): a digit
  • [\s.]+ - one or more whitespace or . chars
  • (?=\d) - a positive lookahead that ensures the next char is a digit.

See JavaScript demo:

const text = 'This is an 8-string 12.34.5678; and this is another 13-string 1234 5678 9123 0 okay?';
console.log(text.replace(/(\d)[\s.]+(?=\d)/g, '$1'));

How to remove some dots from a string in python

Here is a solution:

p = '23.4565.90'

def rreplace(string: str, find: str, replace: str, n_occurences: int) -> str:
"""
Given a `string`, `find` and `replace` the first `n_occurences`
found from the right of the string.
"""
temp = string.rsplit(find, n_occurences)
return replace.join(temp)

d = rreplace(string=p, find='.', replace='', n_occurences=p.count('.') - 1)

print(d)

>>> 23.456590

Credit to How to replace all occurences except the first one?.

Dataframe: How to remove dot in a string

# The following code should work:
df.NACE_code = df.NACE_code.astype(str)
df.NACE_code = df.NACE_code.str.replace('.', '')

Remove Dot from String

string number= "2.36"; 
string newNumber = number.Replace(".", "");

Python regex remove dots from dot separated letters

Starting the match with a . dot not make sure that there is a char a-zA-Z before it.

If you use the named group word in the replacement, that will contain the value of the last iteration as it is by itself in a repeated group.


You can match 2 or more dots with 1 or 2 times a char a-zA-Z and replace the dots with an empty string when there is a match instead.

To prevent aaa.b.cc from matching, you could make use of word boundaries \b

\b[a-zA-Z]{1,2}(?:\.[a-zA-Z]{1,2}){2,}\b

The pattern matches:

  • \b A word boundary to prevent the word being part of a larger word
  • [a-zA-Z]{1,2} Match 1 or 2 times a char a-zA-Z
  • (?: Non capture group
    • \.[a-zA-Z]{1,2} Match a dot and 1 or 2 times a char a-zA-Z
  • ){2,} Close non capture group and repeat 2 or more times to match at least 2 dots
  • \b A word boundary

Regex demo | Python demo

import re

pattern = r"\b[a-zA-Z]{1,2}(?:\.[a-zA-Z]{1,2}){2,}\b"
texts = [
'a.b.c',
'ab.c.dd.ee',
'a.b',
'aaa.b.cc'
]

for s in texts:
print(re.sub(pattern, lambda x: x.group().replace(".", ""), s))

Output

abc
abcddee
a.b
aaa.b.cc

Remove all the dots but not \in numbers - Java

replaceAll() with the right regex can do it for you.

This uses a negative look-ahead and look-behind to look for a '.' not in the middle of a decimal number.

rM.replaceAll("(?<![\\d])\\.(?![\\d]+)", "")


Related Topics



Leave a reply



Submit