[^a-zA-Z\d\s:]
- \d - numeric class
- \s - whitespace
- a-zA-Z - matches all the letters
- ^ - negates them all - so you get - non numeric chars, non spaces and non colons
[^a-zA-Z\d\s:]
- \d - numeric class
- \s - whitespace
- a-zA-Z - matches all the letters
- ^ - negates them all - so you get - non numeric chars, non spaces and non colons
This should do it:
[^a-zA-Z\d\s:]
RegEx to remove all non alphanumeric characters
Help with RegEx to find non alphanumeric characters
Just a little confused about removing/replacing non-alphanumeric characters
Apex: Is there a way to strip out non-alphanumeric characters from a string? - Salesforce Stack Exchange
If it matters I want to split a string at any non alphanumeric characters, however doing /[^W]/ doesn't give the desired result because the word "don't" is then split into "don" and "t"
Why would:
var replaceChar=str.replace(/[^A-Za-z0-9]/g, '');
replace all non-alphanumeric characters?
Why does (/[A-Za-z0-9]/g, ''); signify them? More like, how? Can someone please give a ELI5 explanation? I saw it on StackOverFlow, but the explanation is going over my head.
I would use the following Regular Expression: '[^a-zA-Z0-9]'
Pattern nonAlphanumeric = Pattern.compile('[^a-zA-Z0-9]');
Matcher matcher = nonAlphanumeric.matcher('Sales - Actual Results (TY)');
system.debug(matcher.replaceAll('')); // output: SalesActualResultsTY
Demo
I know this doesn't exactly answer your question (removing invalid characters from your string), but since you're using Platform Cache, why not use a hash for your key? It's unique and you don't have to worry about which characters are in the string you're generating the hash from.
public String generateHash(String inputString) {
Blob targetBlob = Blob.valueOf(inputString);
Blob hash = Crypto.generateDigest('SHA1', targetBlob);
return EncodingUtil.convertToHex(hash);
}
Example usage:
String reportName = "Sales - Actual Results (TY)";
String cacheKey = generateHash(reportName);
// Value of cacheKey is "050bc0bde14099279e556202652e982c8a47f2b8"
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_crypto.htm