Question

I am preparing for Oracle Certified Java Programmer. I am looking into regular expressions. I was going through javaranch Regular Expression and i am not able to understand the regular expression present in the example. Please help me in understanding it. I am adding source code for reference here. Thanks.

class Test
{
  static Map props = new HashMap();
  static
  {
    props.put("key1", "fox");
    props.put("key2", "dog");
  }

  public static void main(String[] args)
  {
    String input = "The quick brown ${key1} jumps over the lazy ${key2}.";

    Pattern p = Pattern.compile("\\$\\{([^}]+)\\}");
    Matcher m = p.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (m.find())
    {
      m.appendReplacement(sb, "");
      sb.append(props.get(m.group(1)));
    }
    m.appendTail(sb);
    System.out.println(sb.toString());
  }
}
Was it helpful?

Solution 2

Here is a very good tutorial on regular expressions you might want to check out. The article on quantifiers has two sections "Laziness instead of Greediness" and "An Alternative to Laziness", that should explain this particular example really well.

Anyway, here is my explanation. First, you need to realize that there are two compilation steps in Java. One compiles the string literal in your code to an actual string. This step already interprets some of the backslashes, so that the string Java receives looks like

\$\{([^}]+)\}

Now let's pick that apart in free-spacing mode:

\$      # match a literal $
\{      # match a literal {
(       # start capturing group 1
  [^}]  # match any single character except } - note the negation by ^
  +     # repeat one or more times
)       # end of capturing group 1
\}      # match a literal }

So this really matches all occurrences of ${...}, where ... can be anything except closing }. The contents of the braces (i.e. the ...) can later be accessed via m.group(1), as it's the first set of parentheses in the expression.

Here are some more relevant articles of the above tutorial (but you should really read it in its entirety - it's definite worth it):

OTHER TIPS

An illustration of your regex:

\$\{([^}]+)\}

Regular expression image

Edit live on Debuggex

  • \\$: matches a literal dollar sign. Without the backslashes, it matches the end of a string.
  • \\{: matches a literal opening curly brace.
  • (: start of a capturing group
    • [^}]: matches any character that isn't a closing curly brace.
    • +: repeats the last character set, which will match one or more characters that aren't curly braces.
  • ): closing capturing group.
  • \\}: matches a literal closing curly brace.

It matches stuff that looks like ${key1}.

Explanation:

\\$         literal $ (must be escaped since it is a special character that 
            means "end of the string"
\\{         literal { (i m not sure this must be escaped but it doesn't matter)
(           open the capture group 1
  [^}]+     character class containing all chars but }
)           close the capture group 1
\\}         literal }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top