14
votes
Accepted
using regex in replaceAll to replace multiple char in one go
Try this:
String r = t.replaceAll('[^0-9]','');
Explanation:
Instead of hunting for invalid characters and removing them explicitly you can specify remove all characters other than numbers.
^ for ...
13
votes
Accepted
Replace Line Breaks (Multiple count as 1) in a string
This is a good time for replaceAll(regex, replacement):
String testString = 'Line 1\r\nLine 2\r\n\r\nLine 3';
system.debug(testString.replaceAll('(\\r|\\n)+', ' ** '));
Results:
Line 1 ** Line 2 *...
13
votes
Accepted
lightning:input (text) - is it possible to have multiple patterns
Use the "OR" operator (|):
pattern="(\d{8}|[a-zA-Z0-9]{12}-\d{3})"
12
votes
Accepted
How to get running class and method names programmatically?
Here's a fun fact to get the ball rolling: the constructor for any Exception class will generate the current stack trace (no need to throw). So you can, for instance, get the stack trace in your ...
10
votes
How can I convert my htmlBody to plainTextBody using Apex/Regex
I think rather than using any RegEx, you can just use a built in String method: stripHtmlTags.
Salesforce String Class
So it would look like:
String plainText = htmlBody.stripHtmlTags();
10
votes
Accepted
Replacing white spaces is not working - replaceAll('\\s+'
The replaceAll method returns the modified string, it does not modify the reference to the value passed.
So you should change it to this and it should work for you:
a = a.replaceAll('\\s+', '-');
c =...
10
votes
Regex Help- Escaping Characters And Matcher Method
The simplest solution is to use the \W character as your entire expression, it matches any non-word character. Unfortunately, this solution would also match any whitespace characters, and ignore the ...
10
votes
Accepted
Regex Help- Escaping Characters And Matcher Method
You can match all punctuation using \\p{Punct}, as mentioned in the Pattern class, which matches:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
For example, the following code results in an empty ...
9
votes
Accepted
Regular Expression matches returns false
The documentation on the Matcher class should shed some light here.
The description for matches() states the following
Attempts to match the entire region against the pattern.
Meanwhile the ...
8
votes
Accepted
Apex Regex Extract Numeric Value from String
You are using a capturing group so this works as you expect: (You need to use find):
Pattern pat = Pattern.compile('([0-9]+)');
Matcher matcher = pat.matcher('red fox 11133434 red fox');
...
8
votes
Accepted
Split by /* act as regex?
Split actually expects a regular expression, so your split string is interpreted as "split when there are zero or more / characters" ("*" means "zero or more").
To avoid ...
8
votes
Accepted
Lightning component regex doesn't work
The reason for this seems to be that the framework replaces the first line's asterisk character with the \u002A Unicode escape sequence before serving the files to the browser.
I'd report a bug if it ...
8
votes
Regex - Syntax error
Escape the \ with an extra \ . Like below:
AND(
!ISBLANK( Phone ),
!REGEX( Phone , '(([+][(]?[0-9]{1,3}[)]?)|([(]?[0-9]{4}[)]?))\\s*[)]?[-\\s\\.]?[(]?[0-9]{1,3}[)]?([-\\s\\.]?[0-9]{3})([-\\s\\.]?[0-...
7
votes
Accepted
Regex including curly brackets
Since backslashes are the escape character in Apex string literals, if you want a literal backlash, you need to escape it with a backslash.
So the right regex to use would be \\{.*\\}
7
votes
Regular Expression matches returns false
It does work - but only if you use the method that yields a substring match.
String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
String regex = '(\\d{11}|\\d{5} \\d{6}|\\+\\d{...
7
votes
Accepted
How to emulate SOQL LIKE matching in Apex?
Couldn't find code for this anywhere - many examples ignore the possibility of characters that have special meaning in regular expressions being present.
This is what I ended up with as a solution, ...
7
votes
Accepted
SOQL query to fetch records with reserved characters ('*', '/', '(', ')')
The * character is not a special character as far as SOQL is concerned. Here's a trivial proof for you:
And here's the same in Apex:
System.debug(
Database.query(
'SELECT Name FROM ...
6
votes
Accepted
Regex: Make Dot Match Newline?
Different languages use different flags to control inline behavior. I gather that Ruby uses m to make dot match newline, but in Apex (and most other languages too, I think) you would want to use the s ...
6
votes
Accepted
Regex matches not being found...but they are there
I tested your regular expression on RegexPlanet, and I found that it agrees with the behavior that salesforce is presenting:
Regular Expression (?mi)(\d)(?=\sNight)
as a Java string "(?mi)(\\d)(?=...
6
votes
Replacing white spaces is not working - replaceAll('\\s+'
Eric has a good answer, but this will also work for this specific case, just a generic method for what you're trying to do, this will return a working format for POST to google
public static String ...
6
votes
Accepted
Issues with Regex matcher on APEX
There's two issues here: one is escaping the regex correctly, and one is the semantics of checking for a match. The following code works:
Pattern regex = Pattern.compile('(?m)([A-Z]{2,3})[\\s]+([0-9]+[...
6
votes
Accepted
Invalid string literal '^.*\s(\d+)\s.*$'. Illegal character sequence \s' in string literal
Escape all the \ characters with more \ character of course!
private final String regex = '^.*\\s(\\d+)\\s.*$';
6
votes
Accepted
Regex implementation in Salesforce: getting from known string to end of the line
You need to turn on multi-line mode in order for the $ specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m).
As Derek F. ...
6
votes
Accepted
How do I replace all non-digit characters in a regex except for the period?
You can write a negated character class with ^ to include the period, ., and the set of digits, 0-9:
var z = x.replace(/[^0-9.]/g, '');
z is then "100.00".
6
votes
Accepted
Using String.replaceAll() to remove character combination
You could use replaceAll with a regular expression as follows:
url = url.replaceAll('-+','-');
This says "match one or more (+) hyphens (-)" and we replace it with the singular hyphen (-).
6
votes
Accepted
Why Salesforce does not accept my REGEX?
That's because SFDC matches the whole field against your REGEX, your whole Email__c is not only ';.+$', plus $ is a special character, so you need something like this
REGEX( Email__c, '.*[;.+$].*')
If ...
5
votes
Accepted
Regex problems in Apex
You are getting this error message because Apex uses ' to define strings. In your expression, you have this character appearing multiple times, breaking the code. You should just add a \ before them. ...
5
votes
Accepted
regular expression problem (Pattern/Matcher)
The matches method will only return true if the entire input string matches the result. You want to use find instead, which will match a substring. Take a look at the Matcher documentation for these ...
5
votes
Accepted
How to create SUBSTITUTE function formula using REGEX
The formula syntax is not powerful enough to do as you wish. You would necessarily need to resort to a simple Apex Trigger. At a basic level, it would look something like this:
trigger ...
5
votes
Accepted
Regular expression to check special symbols not working in lightning javascript
Try using the constructor syntax.
I've verified that this saves in lightning:
var regularExpression;
var re = new RegExp('[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]');
Note the escape of the ' ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regular-expressions × 351apex × 163
validation-rule × 50
validation × 42
string × 39
pattern × 32
matcher × 29
formula × 19
javascript × 17
marketing-cloud × 12
lightning-web-components × 10
visual-workflow × 10
ampscript × 9
lightning-aura-components × 8
lightning × 8
apex-email-service × 8
replaceall × 8
visualforce × 6
trigger × 6
email × 6
soql × 5
replace × 5
process-builder × 4
json × 4
code × 4