英文:
Remove all the dots but not \in numbers - Java
问题
我正在尝试替换字符串中除了像 1.02 这样的数字之外的所有 .
我有一个字符串:-
String rM = "提供了51.3升水。使用了23.3升。"
如果我使用 rM.replaceAll(),那么所有的句点都会被替换,我希望我的字符串是:-
51.3升水已被提供 23.3升已被使用
在Java中是否有可能实现?
英文:
I am trying to replace all the . in a string except numbers like 1.02
I have a string : -
String rM = "51.3L of water is provided. 23.3L is used."
If I use rM.replaceAll() then every dot will be replaced, I want my string to be : -
51.3L of water is provided 23.3L is used
Is it possible to do in java?
答案1
得分: 3
我不是Java开发人员,但你可以尝试以下类似的模式。
rM = rM.replaceAll("(?<=[a-z\\s])\\.", "");
英文:
I am not a java developer but can you try it with a pattern like below.
rM = rM.replaceAll("(?<=[a-z\\s])\\.", "");
答案2
得分: 1
是的,这是可能的。类似以下的代码应该可以工作。正则表达式应该只检查element是否以字符0-9开始。如果是,不要改变这个元素。如果不是,将任何 . 替换为空字符串。
        String rM = "51.3L的水已提供。使用了23.3L。";
        String[] tokens = rM.split(" ");
        StringBuffer buffer = new StringBuffer();
        for (String element : tokens) {
            if (element.matches("[0-9]+.*")) {
                buffer.append(element + " ");
            } else {
                buffer.append(element.replace(".", "") + " ");
            }
        }
        System.out.println(buffer.toString());
输出:
51.3L的水已提供。使用了23.3L。 
英文:
yes its possible. Something like the following should work. The regex should just check that the element starts with a character 0-9. If yes, don't change the element. If no, replace any . with the empty string.
        String rM = "51.3L of water is provided. 23.3L is used.";
        String[] tokens = rM.split(" ");
        StringBuffer buffer = new StringBuffer();
        for (String element : tokens) {
            if (element.matches("[0-9]+.*")) {
                buffer.append(element + " ");
            } else {
                buffer.append(element.replace(".", "") + " ");
            }
        }
        System.out.println(buffer.toString());
Output:
51.3L of water is provided 23.3L is used 
答案3
得分: 1
这是一个简单的方法,假设您想要去掉紧跟在非空白字符后面的点。
以下代码基本上通过空格将句子拆分为单词,并删除每个结果字符序列中的尾随点,然后将它们重新连接成单个字符串。
public static void main(String[] args) {
	// 示例句子
	String rM = "51.3L of water is provided. 23.3L is used.";
	// 通过空格拆分句子
	String[] parts = rM.split("\\s+");
	
	// 遍历所有部分
	for (int i = 0; i < parts.length; i++) {
		// 检查部分是否以点结尾
		if (parts[i].endsWith(".")) {
			// 如果是,将该部分替换为去掉末尾点的部分
			parts[i] = parts[i].substring(0, parts[i].length() - 1);
		}
	}
	
	// 将部分重新连接为句子字符串
	String removedUndesiredDots = String.join(" ", parts);
	// 并打印输出
	System.out.println(removedUndesiredDots);
}
输出为
51.3L of water is provided 23.3L is used
英文:
Here's a simple approach that assumes you want to get rid of dots that are placed directly after a char which isn't a whitespace.
The following code basically splits the sentence by whitespace(s) and removes trailing dots in every resulting character sequence and joins them afterwards to a single String again.
public static void main(String[] args) {
	// example sentence
	String rM = "51.3L of water is provided. 23.3L is used.";
	// split the sentence by whitespace(s)
	String[] parts = rM.split("\\s+");
	
	// go through all the parts
	for (int i = 0; i < parts.length; i++) {
		// check if one of the parts ends with a dot
		if (parts[i].endsWith(".")) {
			// if it does, replace that part by itself minus the trailing dot
			parts[i] = parts[i].substring(0, parts[i].length() - 1);
		}
	}
	
	// join the parts to a sentence String again
	String removedUndesiredDots = String.join(" ", parts);
	// and print that
	System.out.println(removedUndesiredDots);
}
The output is
51.3L of water is provided 23.3L is used
答案4
得分: 1
replaceAll()方法与正确的正则表达式可以为您完成此操作。
这里使用了负向先行断言和负向后行断言来查找不在十进制数中间的'.'。
rM.replaceAll("(?<![\\d])\\.(?![\\d]+)", "")
英文:
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]+)", "")
答案5
得分: 1
使用负向先行断言,您可以使用\.(??)。
private static final String DOTS_NO_NUM_REGEX = "\\.(??)";
private static final Pattern PATTERN = Pattern.compile(DOTS_NO_NUM_REGEX);
public static void main(String[] args) {
    String s = "51.3L of water is provided. 23.3L is used.";
    String replaced = PATTERN.matcher(s).replaceAll("");
    System.out.println(replaced);
}
输出:
51.3L of water is provided 23.3L is used
英文:
Using negative lookahead you can use \.(??).
private static final String DOTS_NO_NUM_REGEX = "\\.(??)";
private static final Pattern PATTERN = Pattern.compile(DOTS_NO_NUM_REGEX);
public static void main(String[] args)
{
    String s = "51.3L of water is provided. 23.3L is used.";
    String replaced = PATTERN.matcher(s).replaceAll("");
    System.out.println(replaced);
}
Output:
51.3L of water is provided 23.3L is used
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论