21
votes
Accepted
Catch Regex too complicated when splitting a string in salesforce
Not a catch per say (IIRC you cannot catch it, especially if Catch Exception did not work) but will solve the problem of regex to complex, use a custom iterator:
CLASS
public with sharing class ...
- 53.6k
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 ...
- 2,406
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 *...
- 146k
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})"
- 456k
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 ...
- 146k
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 =...
- 53.6k
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 ...
- 146k
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 ...
- 456k
9
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();
- 2,208
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 ...
- 53.4k
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');
...
- 53.6k
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 ...
- 456k
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 ...
- 383
7
votes
Accepted
7
votes
Accepted
replace characters while keeping original parts
Use grouping to grab the value that was found, then use it in the replacement by re-calling it with the $1.
Example
// note the parentheses for grouping in the pattern
String regExp = '([\\d])';
...
- 2,648
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 \\{.*\\}
- 53.4k
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{...
- 89.5k
7
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-...
- 1,850
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, ...
- 132k
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)(?=...
- 456k
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 ...
- 2,537
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 ...
- 4,327
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]+[...
- 89.5k
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. ...
- 89.5k
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 (-).
- 456k
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 ...
- 4,320
6
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 ...
- 456k
5
votes
Accepted
Validation Rule to count words
Did you mean \w*? Currently, your regex is returning a pattern (true) if it finds whitespace then a nonword character...your use case is perfectly reasonable for regex and if it's exceeding runtime ...
- 6,342
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. ...
- 11.6k
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 ...
- 146k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regular-expressions × 321apex × 152
validation-rule × 41
validation × 38
string × 35
pattern × 29
matcher × 26
formula × 19
javascript × 14
marketing-cloud × 11
lightning-aura-components × 8
ampscript × 8
visual-workflow × 8
apex-email-service × 8
replaceall × 8
lightning × 7
lightning-web-components × 7
visualforce × 6
trigger × 5
soql × 5
email × 5
replace × 5
process-builder × 4
json × 4
code × 4