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 ...
karthikselva's user avatar
  • 2,446
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
  • 149k
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
  • 481k
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
  • 149k
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
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 =...
Eric's user avatar
  • 54k
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
  • 149k
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
  • 481k
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
  • 59.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'); ...
Eric's user avatar
  • 54k
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
  • 481k
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

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
  • 59.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{...
David Reed's user avatar
  • 92.1k
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
  • 135k
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
  • 481k
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 ...
RenegadeCoder's user avatar
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)(?=...
sfdcfox's user avatar
  • 481k
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 ...
EricSSH's user avatar
  • 4,324
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
  • 92.1k
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,187
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
  • 92.1k
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
  • 92.1k
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
  • 481k
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,475
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. ...
Renato Oliveira's user avatar
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
  • 149k
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
  • 481k
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

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