Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace last dot in a string using a regular expression?

Tags:

java

string

regex

I'm trying to replace the last dot in a String using a regular expression.

Let's say I have the following String:

String string = "hello.world.how.are.you!";

I want to replace the last dot with an exclamation mark such that the result is:

"hello.world.how.are!you!"

I have tried various expressions using the method String.replaceAll(String, String) without any luck.

like image 542
digiarnie Avatar asked Nov 04 '10 05:11

digiarnie


People also ask

How do you replace a dot in a string?

To replace the dots in a string, you need to escape the dot (.) and replace using the replace() method.

How do you remove the last dot from a string in Java?

chop() Method. The StringUtils class provides a chop() method to remove the last character from a string.

What does\\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .

What is regex in str replace?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.


4 Answers

One way would be:

string = string.replaceAll("^(.*)\\.(.*)$","$1!$2");

Alternatively you can use negative lookahead as:

string = string.replaceAll("\\.(?!.*\\.)","!");

Regex in Action

like image 92
codaddict Avatar answered Sep 17 '22 15:09

codaddict


Although you can use a regex, it's sometimes best to step back and just do it the old-fashioned way. I've always been of the belief that, if you can't think of a regex to do it in about two minutes, it's probably not suited to a regex solution.

No doubt get some wonderful regex answers here. Some of them may even be readable :-)

You can use lastIndexOf to get the last occurrence and substring to build a new string: This complete program shows how:

public class testprog {
    public static String morph (String s) {
        int pos = s.lastIndexOf(".");
        if (pos >= 0)
            return s.substring(0,pos) + "!" + s.substring(pos+1);
        return s;
    }
    public static void main(String args[]) {
        System.out.println (morph("hello.world.how.are.you!"));
        System.out.println (morph("no dots in here"));
        System.out.println (morph(". first"));
        System.out.println (morph("last ."));
    }
}

The output is:

hello.world.how.are!you!
no dots in here
! first
last !
like image 27
paxdiablo Avatar answered Sep 17 '22 15:09

paxdiablo


The regex you need is \\.(?=[^.]*$). the ?= is a lookahead assertion

"hello.world.how.are.you!".replace("\\.(?=[^.]*$)", "!")
like image 29
John La Rooy Avatar answered Sep 19 '22 15:09

John La Rooy


Try this:

string = string.replaceAll("[.]$", "");
like image 45
panticz Avatar answered Sep 21 '22 15:09

panticz