-1

Method to filter Map where regex ($) is matched :

public Map getVariables(Map<String , String> nvp){

        Map map = nvp.entrySet().parallelStream()
                .filter(e -> e.getKey().matches("(($.*?))"))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        return map;

    }

Setup a map that contains key/value "($test)", "test value" :

private Map<String , String> nvp = new HashMap<String , String>();
public void setup(){
    nvp.put("($test)", "test value");
}

Add a method to test that "($test)" is contained in the Map returned :

public void testGetVariables(){

    OpDocVariableInsert o = new OpDocVariableInsert();
    System.out.println(o.getVariables(nvp).size());
}

But 0 results are returned. I think my regex is incorrect ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 1
    `$` has a special meaning in regex, you should escape it: `\\$`. Also note that you should escape the `(` and `)` if you didn't mean to capture the match. – Maroun Mar 16 '16 at 15:03
  • Note `\\(\\$[^)]++\\)` would be a more efficient pattern than using a lazy `.`. – Boris the Spider Mar 16 '16 at 15:07

1 Answers1

2

$ and () have special meanings in regex and should be escaped to match their original characters :

\\(\\$.*?\\)
Aaron
  • 24,009
  • 2
  • 33
  • 57