2022 Developer Survey is open! Take survey.
22 votes
Accepted

Making regex case-insensitive

You need to use JAVA-like CASE_INSENSITIVE pattern (?i) Something like this: Pattern staPattern = Pattern.compile('(?i)STA[0-9]{4}');
user avatar
  • 21.7k
20 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 ...
user avatar
  • 53.1k
17 votes

Best REGEX for first/last name validation?

Yes! Let's validate some names with RegEx. After all, we know that all people must have a first and last name, right? And no single person has more than three or four names total? And no doubt the ...
user avatar
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 ...
user avatar
  • 2,396
12 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 *...
user avatar
  • 142k
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 ...
user avatar
  • 142k
11 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})"
user avatar
  • 430k
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 =...
user avatar
  • 53.1k
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 ...
user avatar
  • 142k
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 ...
user avatar
  • 430k
9 votes
Accepted

How to replace 'MyFirstName' by 'My First Name'

I will answer to my own question : String text = 'ILikeSalesforcePlatform'; text = String.valueOf(text.splitByCharacterTypeCamelCase()).replaceAll('[\\,\\(\\)]',''); System.debug(text); Output : ...
user avatar
  • 5,478
9 votes
Accepted

How to split a string by comma and white space

This should work for you: String x = 'a, b,c'; system.debug(x.split('[,]{1}[\\s]?')); --KC
user avatar
  • 481
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 ...
user avatar
  • 46.8k
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'); ...
user avatar
  • 53.1k
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 ...
user avatar
  • 430k
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 ...
user avatar
7 votes
Accepted

How to find all numbers in a string using Regex

You need to call matcher.find() recursively until it returns false. Use a do/while block. String str = '123-456/7890'; Pattern p = Pattern.compile('(\\d+)'); Matcher m = p.matcher( str ); if(m.find()...
user avatar
  • 3,959
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])'; ...
user avatar
  • 2,618
7 votes
Accepted

Regex in the validation rule

Try this RegEx [0-9]{6}(;[0-9]{6})*
user avatar
7 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();
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 \\{.*\\}
user avatar
  • 46.8k
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{...
user avatar
  • 86.2k
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-...
user avatar
6 votes
Accepted

Regex to validate Credit Card numbers

This is really more of a regex question than a SFDC question, so it truly belongs elsewhere in StackExchange. However, if you know that your patterns work independently, you could change the ...
user avatar
  • 22.3k
6 votes

How to find all numbers in a string using Regex

If you want to separate all the numbers into separate strings you can do the following. String numsplit = str.replaceAll('[^0-9]+', ';'); list<String> nums = numsplit.split(';'); If you also ...
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)(?=...
user avatar
  • 430k
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 ...
user avatar
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 ...
user avatar
  • 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]+[...
user avatar
  • 86.2k
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. ...
user avatar
  • 86.2k

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