Paul Dumitru
Paul Dumitru

Reputation: 68

Java RegExp Replace

Hello I've been trying to make some replacement with not success

public class demo {

    public static void main(String[] args){
        String url = "/demoapi/api/user/123";
        String newurl = "/user/?user=$1";

        Pattern pattern = Pattern.compile("/^\\/demoapi\\/api\\/user\\/([0-9]\\d*)$/i");
        Matcher match = pattern.matcher(url);
    }

}

I want to replace $1 with 123 , how do I do this ?! Thank you !

Upvotes: 0

Views: 266

Answers (5)

zx81
zx81

Reputation: 41838

Compact Search: .*?(\d+)$

This is all you need:

String replaced = yourString.replaceAll(".*?(\\d+)$", "/user/?user=$1");

In the regex demo, see the substitutions at the bottom.

Explanation

  • (\d+) matches one or more digits (this is capture Group 1)
  • The $ anchor asserts that we are at the end of the string
  • We replace with /user/?user= and Group 1, $1

Upvotes: 0

OO7
OO7

Reputation: 2807

You can use any of the following method :-

public class Test {
public static void main(String[] args) {
    public static void main(String[] args) {

    String url = "/demoapi/api/user/123";
    String newurl = "/user/?user=$1";

    String s1 = newurl.replaceAll("\\$1", Matcher.quoteReplacement("123"));
    System.out.println("s1 : " + s1);
    // OR
    String s2 = newurl.replaceAll(Pattern.quote("$1"),Matcher.quoteReplacement("123"));
    System.out.println("s2 : " + s2);
    // OR
    String s3 = newurl.replaceAll("\\$1", "123");
    System.out.println("s3 : " + s3);
    // OR
    String s4 = newurl.replace("$1", "123");
    System.out.println("s4 : " + s4);       
}

}

Explanation of Methods Used :

Pattern.quote(String s) : Returns a literal pattern String for the specified String. This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern. Metacharacters or escape sequences in the input sequence will be given no special meaning.

Matcher.quoteReplacement(String s) : Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

String.replaceAll(String regex, String replacement) : Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

Pattern.compile(regex).matcher(str).replaceAll(repl)

Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

String.replace(CharSequence target, CharSequence replacement) : Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

You don't need to enter the whole text ^\\/demoapi\\/api\\/user\\ in the pattern. Just a ^.*\\/ will match upto the last / symbol. So Your java code would be,

String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
String m1 = url.replaceAll("(?i)^.*\\/([0-9]+)$", "$1");
String m2 = newurl.replaceAll("\\$1", m1);
System.out.println(m2);

Output:

/user/?user=123

Explanation:

  • (?i) Turn on the case insensitive mode.
  • ^.*\\/ Matches upto the last / symbol.
  • ([0-9]+)$ Captures the last digits.

IDEONE

OR

String url = "/demoapi/api/user/123";
String m1 = url.replaceAll(".*/(\\d*)$", "/user/?user=$1");

You need to put / before (\\d*), so that it would capture the numbers from starting ie, 123. Otherwise it would print the last number ie, 3.

Upvotes: 0

Braj
Braj

Reputation: 46841

I want to replace $1 with 123 , how do I do this ?!

Simply use replace method but never forget to escape $

"/user/?user=$1".replace(/(\$1)/,"123");

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

I think you are looking for something like this:

   String url = "/demoapi/api/user/123";
    String newurl = "/user/?user=$1";

    Pattern pattern = Pattern.compile(".*/user/(\\d*)");
    Matcher match = pattern.matcher(url);
    if(match.matches()){
        newurl = newurl.replace("$1", match.group(1));
    }

    System.out.println(newurl);

Hope this helps.

Upvotes: 0

Related Questions