regex - Java pattern Match for a specific pattern to match -


i try match pattern in given string static, following program:

package com.test.poc; import java.util.regex.matcher; import java.util.regex.pattern;  public class regextestpatternmatcher {   public static final string example_test = "http://localhost:8080/api/upload/form/{uploadtype}/{uploadname}";    public static void main(string[] args) {     pattern pattern = pattern.compile("{\\w+}");     // in case ignore case sensitivity use     // statement     // pattern pattern = pattern.compile("\\s+", pattern.case_insensitive);     matcher matcher = pattern.matcher(example_test);     // check occurance     while (matcher.find()) {       system.out.print("start index: " + matcher.start());       system.out.print(" end index: " + matcher.end() + " ");       system.out.println(matcher.group());     }     // create new pattern , matcher replace whitespace tabs     pattern replace = pattern.compile("\\s+");     matcher matcher2 = replace.matcher(example_test);     system.out.println(matcher2.replaceall("\t"));   } }  

i try match strings available in {} , replace them value.

but gives me exception :

exception in thread "main" java.util.regex.patternsyntaxexception: illegal repetition {\w+}     @ java.util.regex.pattern.error(pattern.java:1713)     @ java.util.regex.pattern.closure(pattern.java:2775)     @ java.util.regex.pattern.sequence(pattern.java:1889)     @ java.util.regex.pattern.expr(pattern.java:1752)     @ java.util.regex.pattern.compile(pattern.java:1460)     @ java.util.regex.pattern.<init>(pattern.java:1133)     @ java.util.regex.pattern.compile(pattern.java:823)     @ com.test.poc.regextestpatternmatcher.main(regextestpatternmatcher.java:9) 

what problem make here. sorry ask here

{ , } reserved characters. need escape those: \\{ , \\}

for reference, these characters used repetition.

http://www.regular-expressions.info/repeat.html

edit

i don't believe can simple replacement using style of matching. can build new string, successively looking each match:

public static void main( string[] args ) {     string toconvert = example_test;      pattern pattern = pattern.compile("\\{\\w+\\}");     // in case ignore case sensitivity use     // statement     // pattern pattern = pattern.compile("\\s+", pattern.case_insensitive);     matcher matcher = pattern.matcher(toconvert);     stringbuilder resultstringbuilder = new stringbuilder();     int startpos = 0;     while (matcher.find()) {          // append match.         resultstringbuilder.append(toconvert.substring(startpos, matcher.start()));          // append replacement         resultstringbuilder.append(lookup(matcher.group()));          // set start pos next match         startpos = matcher.end();     }     // append that's left.     resultstringbuilder.append(toconvert.substring(startpos, toconvert.length()));               string resultstrig = resultstringbuilder.tostring();     system.out.println(resultstrig); }   private static string lookup( string s ) {     // decide want replace string     // might want make use of treemap<string,string> here.     return ""; } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -