Java split and replace a string with value

I have a following string which I have to split and replace the value on some condition


in which I have to replace the value only for {uploadType}/{uploadName} . I really don't know how to go about to replace them with value.

The strings which are in {uploadType}/{uploadName} can be of any type to replace.

I've tried something like the following :

package com.test.poc;

public class TestString {
public static void main(String[] args) throws ClassNotFoundException {
    String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
    String[] toReplaceWith =toBeFixed.split("{");
for (String string : toReplaceWith) {
    System.out.println("string : "+string);


but i get the following exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
    at java.util.regex.Pattern.error(
    at java.util.regex.Pattern.closure(
    at java.util.regex.Pattern.sequence(
    at java.util.regex.Pattern.expr(
    at java.util.regex.Pattern.compile(
    at java.util.regex.Pattern.<init>(
    at java.util.regex.Pattern.compile(
    at java.lang.String.split(
    at java.lang.String.split(
    at com.test.poc.TestString.main(


this is the method i tried based on Sean Patrick Floyd answer

public String doPOSTPathVariable(String uri,List paramsList) throws IllegalArgumentException, IllegalAccessException, InstantiationException, Exception{
        String uriString="";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
        for (int j = 0; j<= paramsList.size(); j++) {
            System.out.println("path variable");
            MethodParams methodParams;
            methodParams =(MethodParams) paramsList.get(j);

            if(methodParams.isPrimitive() && methodParams.getDataType()=="boolean"){
                  uriString = uriComponents.expand(true).encode().toUriString();
            if(methodParams.isPrimitive() && methodParams.getDataType()=="java.math.BigDecimal"){
                uriString = uriComponents.expand(123).encode().toUriString();
            if(methodParams.isPrimitive() && methodParams.getDataType()=="java.lang.String"){
                uriString = uriComponents.expand("hexgen").encode().toUriString();
        return uriString;

but i get following exception :

java.lang.IllegalArgumentException: Not enough variable values available to expand 'uploadName'
    at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(
    at org.springframework.web.util.UriComponents.expandUriComponent(
    at org.springframework.web.util.UriComponents.access$1(
    at org.springframework.web.util.UriComponents$FullPathComponent.expand(
    at org.springframework.web.util.UriComponents.expandInternal(
    at org.springframework.web.util.UriComponents.expand(
    at com.hexgen.reflection.HttpClientRequests.handleHTTPRequest(
    at com.hexgen.reflection.HexgenWebAPITest.main(

Could some one help me on this please?


A { is a regex meta-character used for range repetitions.

before doing that

toBeFixed = toBeFixed.replaceAll("\\{", "\n");   

see docs also :

Similar question :Java String ReplaceAll method giving illegal repetition error?

Perhaps the UriBuilder?

UriBuilder.fromPath("http://localhost:8080/api/upload/form/{uploadType}/{uploadName}").build("foo", "bar");

(Conveniently happens to use the exact formatting you are using)

If you use Spring MVC, this functionality is available out of the box with the new URI builder technology.

UriComponents uriComponents =

URI uri = uriComponents.expand("42", "21").encode().toUri();
// or:
String uriString = uriComponents.expand("42", "21").encode().toUriString();

(In fact you can still use this technology, even if you don't use Spring MVC, but you'll need to have the Spring MVC jar on the Classpath, obviously)

You can try this one :

public class TestString {
public static void main(String[] args) throws ClassNotFoundException {
String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
String[] toReplaceWith =toBeFixed.split("\\{");
for (String string : toReplaceWith) {
System.out.println("string : "+string);


UrlBuilder is the solution, but if you are just looking around delimiters, you can also use substrings:

String url = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
int lastSlash = url.lastIndexOf('/');
int secondLastSlash = url.substring(0, lastSlash).lastIndexOf('/');
System.out.println(url.substring(secondLastSlash+1, lastSlash));

To remove the curly braces, you can use String.replace('{', '') and String.replace('}', '') on the strings

Need Your Help

NullPointerException on cursor RawQuery - Android

java android sqlite cursor nullpointerexception

I am trying to create a db, insert into the db, pull from the db and display the contents (long) to a TextView.

pass arguments to function in a boost::thread

c++ multithreading boost

This is my first time using the boost thread function and prior to this I have little knowledge of working with multiple threads. I attempting to run a second instance of a function alongside the i...