Question: I am trying to change some values in my json file but it has no effect in the json file even though it prints the changes I have made below.

The json file:
--------------

```
{
"schemaVersion":1,
"array":[  
  { //values...
  },
  { //the relevant values..
    "id":"stackoverflow",
    "visible":true,
  }
 ]
} 

```

The json file is valid I just wrote the relevant things.

Boost code:
-----------

```
boost::property_tree::ptree doc;
string test = dir_path.string();
boost::property_tree::read_json(test, doc);

BOOST_FOREACH(boost::property_tree::ptree::value_type& framePair2, doc.get_child("array")){
   if (!framePair2.second.get<std::string>("id").compare("stackoverflow")){
        cout << framePair2.second.get<std::string>("id") << endl;
        cout << framePair2.second.get<std::string>("visible") << endl;
        framePair2.second.put<string>("visible", "false");
        cout << framePair2.second.get<std::string>("visible") << endl;
   }

```

The output:
-----------

```
stackoverflow //which is fine
true          //which is also fine
false         //which is exactly what I changed and need

```

The problem:
------------

There are no changes in the json file **even** though it prints a success change via `framePair2.second.put<string>("visible", "false");` and I don't understand why.

How can it be that it prints `false` **after** i use *put* method and in the json file it is still `true` ? Do I need to save the json file ? if so, what is the command for it using boost ?

Any help would be appreciated.

Thank you.

Answer: 
I do not understand your reasoning why you want to use replace. Why go that way in the first place? There are methods in the `Regex` class that allow you to precisely get all the desired matches. Your roundabout way at getting to your solution I find is pointless.

Just use `Matches()` to collect the matches. You could then join them into the string that you wanted.

```
var str = "50% of 50% is 25%";
var re = new Regex(@"\d+%");
var ms = re.Matches(str);
var values = ms.Cast<Match>().Select(m => m.Value);
var joined = String.Join("", values); // "50%50%25%"

```
One solution is to use regex replace as follows:

`Regex.Replace("50% of 50% is 25%", "(\d+\%)|(?:.+?)", "$1");`

Output:

`50%50%25%`

As a general approach:

`Regex.Replace(input, (pattern)|(?:.+?), "$1");`

This finds anything that matches either of the following:

* The pattern. Captured as `$1`. This is what we want to keep.
* Any character, any number of times, but non-greedy. This finds anything that is *not* captured by the first group. `?:` because we don't need to capture this group.

As MSDN states: "`$1` replaces the entire match with the first captured subexpression." (That is, all matches for that substring, concatenated.)

Effectively, this is the described *regex filter*.