Skip to main content
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 *...
Adrian Larson's user avatar
  • 151k
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})"
sfdcfox's user avatar
  • 502k
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 ...
Adrian Larson's user avatar
  • 151k
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();
techbusinessman's user avatar
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
sfdcfox's user avatar
  • 502k
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 ...
Derek F's user avatar
  • 64.1k
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 ...
neonblitzer's user avatar
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 ...
sfdcfox's user avatar
  • 502k
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-...
Shailesh Patil's user avatar
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 \\{.*\\}
Derek F's user avatar
  • 64.1k
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{...
David Reed's user avatar
  • 93.7k
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, ...
Keith C's user avatar
  • 137k
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 ...
sfdcfox's user avatar
  • 502k
6 votes

Regex Search-and-Replace with Capture Groups?

You can use $# to reference a capture group ($1 is the first, $2 is the second, etc.) For your case I ran this: System.debug('1a2'.replaceAll('(\\d)(\\D)(\\d)', 'first: $1; second: $2, third: $3')); ...
wizulus's user avatar
  • 190
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]+[...
David Reed's user avatar
  • 93.7k
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.*$';
Robs's user avatar
  • 9,426
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. ...
David Reed's user avatar
  • 93.7k
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".
David Reed's user avatar
  • 93.7k
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 (-).
sfdcfox's user avatar
  • 502k
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 ...
ytiq's user avatar
  • 4,711
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
sfdcfox's user avatar
  • 502k
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 ' ...
Caspar Harmer's user avatar
5 votes
Accepted

Bug with parser in lightning bundle definition

For my usecase, I was able to find a workaround by doing a regex from a string instead of raw like so : var myWorkingRegex = new RegExp('/'); since you do not need to bound your regexes in a string ...
Etienne Rocheleau's user avatar
5 votes
Accepted

How to avoid System.StringException: No match found when only finding a substring and not a full match?

You're misreading the logs: the error in your "failure" code comes from calling Matcher.group (because there was no match). Try the following: String str = 'Text Messaging Plan (abc)'; Pattern p = ...
sfdcfox's user avatar
  • 502k
5 votes
Accepted

Regex having issues in Apex

Apex Pattern class behaves like Java Pattern class. According to Java documentation you have to escape backslash if it is present in regex expression The backslash character ('\') serves to ...
Oleksandr Berehovskyi's user avatar
5 votes
Accepted

Handling multiple regex matches in Ampscript

Not a pretty solution, but here's what I use: %%[ SET @text = "<a href=""https://salesforce.stackexchange.com""target=""_blank"">Salesforce</a><a href=""https://stackoverflow.com"" ...
Cameron Robert's user avatar
5 votes
Accepted

Regex for string but at least one character must be a number or letter

You'd just have to specify that there's a requirement of at least one letter or number somewhere in the string. Here's a possible solution: ^(\w| )*[0-9A-Za-z](\w| )*$ This says we must match zero ...
sfdcfox's user avatar
  • 502k
5 votes

RegExMatch doesn't seem to be working correctly

Your regExmatch function is just going to return the match (in rendered HTML in your case), not do the replacements. Your pattern looks fine. %%[ set @regEx = "<(.*?)>" set @str = &...
Adam Spriggs's user avatar
  • 31.1k

Only top scored, non community-wiki answers of a minimum length are eligible