0

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 !

Paul Dumitru
  • 68
  • 1
  • 10

5 Answers5

1

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");
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

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.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

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
zx81
  • 41,100
  • 9
  • 89
  • 105
  • FYI: fixed a small bug and added regex demo and explanation. :) – zx81 Jul 19 '14 at 10:13
  • As seen in regex demo it gives `/user/?user=3` as output & question is about `replace $1 with 123`. – OO7 Jul 19 '14 at 10:44
  • Hello , your example is great however for some reason that I cannot understand it's returning "/user/?user=3" instead of "/user/?user=123" as far as I know the "+" was supposed to be no matter how many but it replaces only one number – Paul Dumitru Jul 19 '14 at 10:45
  • Wow, that's a typo, my mistake. Please change the `.*` to `.*?` (see updated demo). Please let me know if if works and if you have any question. :) – zx81 Jul 19 '14 at 12:03
  • The updated example throws an exception Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 6 .*?\(d+)$ – Paul Dumitru Jul 19 '14 at 13:38
  • You must have a small typo somewhere. :) Look at the output of [this Java demo](http://ideone.com/coEdsl), it works as expected. – zx81 Jul 19 '14 at 21:46
0

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.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

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".

OO7
  • 2,785
  • 1
  • 21
  • 33
  • thank you very much fr your answer , however the problem is that I will have to make this work by it self since I will not always know what that id will be ... – Paul Dumitru Jul 19 '14 at 13:39