That's still a bit convoluted.
I am trying to search and replace all instances in the string that match a pattern: "cell_cccDD" where "c" is a character and "D" is a digit
When you do this
[bla]
you are creating a character class. In the above example, it will match on
only the lower case characters b, l, a. Which makes this,
'/cell_[a-zA-Z0-9]+/is'
slighly inefficient. There's no need for A-Z if you use the i modifer, case insensitive.
Another error (which you corrected:) ^ denotes
beginning of string, $ at the end of a preg denotes
end of string. Your patterns are neither. Exception: when the first character in a class, [^bla], it means anything
not these characters.
Returning to the original quote, when you describe it like you did, you just have to follow through your description:
"cell_cccDD" where "c" is a character and "D" is a digit
$regex = 'cell\_[a-z]+\d+';
$replace = '0.00';
$ReportTemplate = preg_replace("/$regex/i", "$replace", $ReportTemplate);
cell = literal text "cell"
\_ = escape the underscore, in the right contexts it has a special meaning. Might be fine without it, but always escape it in a $regex to be sure.
[a-z]+ = character class of range a-z, + means "one or more of these"
\d+ = one or more digits.
This one is a little broad. Let's say you expect a
specific number of characters following the underscore, and a specific number of digits after that. Using your examples, let's say it's 3 and 2 respectively.
$regex = 'cell\_[a-z]{3}\d{2}';
Or, at least 3/2 or more,
$regex = 'cell\_[a-z]{3,}\d{2,}';
or, at least 3/2 but not more than 7/6.
$regex = 'cell\_[a-z]{3,7}\d{2,6}';