-1

The catch is username can also have $ symbol in it for example $mahanta. My regex is

\${([^}]+)}

But this does not work.As the language is java and the regex expression is String I escaped the $ with another backslash.Even added 2 more backslashes.But gives run-time exception.

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
Subhendu Mahanta
  • 961
  • 1
  • 18
  • 44
  • Escape the first `{` char. To feel more confident, you may also escape the last `}` char, too. – Wiktor Stribiżew Jul 03 '19 at 09:36
  • This will need a little more context. For instance, in a Java String expressing a regex, you typically have double escapes. Also you might need to add you expected input/output. – Mena Jul 03 '19 at 09:36

1 Answers1

0

This works:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\$\\{([^}]+)}");
        Matcher matcher = pattern.matcher("${$mat}");
        System.out.println("matcher = " + matcher);
        boolean matches = matcher.matches();
        System.out.println("matches = " + matches);
        String group = matcher.group(1);
        System.out.println("group = " + group);
    }
}

See Java PatternSyntaxException: Illegal repetition on string substitution? on why that { needs to be escaped.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211