5 votes
Accepted

RegEx pattern matcher issue

Your testing code erroneously uses both matches and find. Try the following: Pattern MyPattern = Pattern.compile('((?:BN|bn)[0-9]{6})'); Matcher MyMatcher = MyPattern.matcher('BN123456 BN'); while(...
  • 31.4k
4 votes

RegEx pattern matcher issue

If you want to make this pattern case insensitive, can use (?i) at the beginning of the pattern. Pattern MyPattern = Pattern.compile('(?i)(BN[0-9]{6})'); Matcher MyMatcher = MyPattern.matcher('1 ...
4 votes
Accepted

Regular Expression is matching non-whole words despite leveraging \b within the expresion

The OR operator matches the left and right sides as independent matches. x|y matches x or y. The \b operator is part of a match that matches a zero-width "character" that marks a transition ...
  • 463k
4 votes
Accepted

Regex to find dd/mm/yyyy

There are two issues: replace(String target, String replacement) doesn't take a regExp as input, but a literal string. You should have used replaceAll(String regExp, String replacement) Strings are ...
  • 5,654
4 votes

Regex for US Federal Tax ID (EIN)

EIN The EIN has the origin code (a two-character prefix) plus a seven-digit number. It is conceptually possible for certain numbers to exist: 11-1111111 22-2222222 33-3333333 44-4444444 55-5555555 66-...
  • 463k
4 votes
Accepted

Compilation of Regex in Apex

The simple answer, as already stated in the comments, is that you need to use double-backslashes to escape things in regex in Apex. So insted of String fileNameRegex = '/^\[~`!@#$%^&*()_+=\[\]\{}|;...
  • 54.9k
3 votes
Accepted

Regex for Case Number, South African Id and Passport

The main problem is likely the ^; in the default mode or DOTALL (single-line mode), this character can only match the very beginning of the string (i.e. the place before the very first character), ...
  • 463k
3 votes
Accepted

Regex to match single to double digits

It's because of how you're using replace. If you step through this, your string becomes: 1 AND 2 OR (13) // Matches 1 in two places {1} AND 2 OR ({1}3) // Matches 2 once {1} AND {2} OR ({1}3) // 13 ...
  • 463k
3 votes
Accepted

Unable to match group in apex regex

You did get four groups, you just didn't check for them. The group() method returns group(0), which matches outer parentheses group. To get the full picture, you'd check out: while(MyMatcher.find()){ ...
  • 463k
3 votes
Accepted

Extract data from string and reframe it into another

You can use String.replaceAll to match patterns String testString = '11, -22|32.45, 34|32, 41|32, 12.12'; String resultString = '{'+testString.replaceAll('(-?[\\d+\\.]+), ?(-?[\\d+\\.]+)','"a&...
  • 463k
2 votes
Accepted

LWC issue with replacing first invalid character

You can filter out the hyphen in a keydown event handler. <lightning-input type="number" name="anotherAmount" label="Another Amount" variant="standard" value=...
  • 463k
2 votes
Accepted

Salesforce Regex - Return List of Matches

Is this possible with Salesforce's limited regex abilities? Salesforce's regular expressions are more powerful than people give it credit for. It's just misunderstood because it's different than ...
  • 463k
2 votes
Accepted

Removing leading zeroes in Date String

If you really want a locally formatted date, you can use Date.parse and Date.format: String shipDate = '04/06/2022'; System.debug(Date.parse(shipDate).format()); However, be aware that Date.parse ...
  • 463k
2 votes

How to exclude Test files From Searches in VS Code?

Search in VS Code doesn't utilize regex in excluding files (see open issue). It is configured with glob patterns. So you can still do the following: files to exclude: *Test.cls This, of course, ...
2 votes
Accepted

Pattern Matcher problem

On the assumption that you are only interested in a value starting "fact." and delimited by surrounding square brackets, I would use a simpler pattern, of the form: \[(fact\.[^]]*)] Note ...
  • 31.4k
2 votes

Extract data from string and reframe it into another

Note that the first split on the pipe (|) is going to give you a list of strings like ['11, -22', '32.45, 34', '32, 41', '32, 12.12'] So to process this further, you'll either need to split() again, ...
  • 54.9k
2 votes
Accepted

Validation Rule not running the whole formula

The correct version of the validation rule in your question is : AND( NOT( REGEX(Example__c, "[A-Za-z0-9]") ), OR( LEN(Example__c) <> 3, LEN(...
  • 1,122
2 votes
Accepted

REGEX in Salesforce Flow to retrieve a substring

If I'm reading your question accurately Then this is a question about using REGEX() to determine if you should bother executing the MID() and FIND() portion. Regex in formulas is only for determining ...
  • 54.9k
2 votes

REGEX in Salesforce Flow to retrieve a substring

The REGEX() formula function is only capable of returning a Boolean; it cannot return the matched string. It's primarily intended for use in Validation Rule formulas. The working formula in your ...
1 vote

Regex not working in class but works in execute anonymous

The Apex compiler requires certain characters to be escaped in order to work. This is mentioned in Primitive Data Types: EscapeSequences: All Strings in Apex use the same escape sequences as SOQL ...
  • 463k
1 vote
Accepted

Regex not working in class but works in execute anonymous

According to doc find() Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher ...
1 vote

REGEX in Salesforce Flow to retrieve a substring

To provide a different option for working with a string that is known to be a URL and its parameters other than RegEx which can be challenging, consider instead using the PageReference class to parse ...
  • 22.7k
1 vote

REGEX in Salesforce Flow to retrieve a substring

Using this formula i was able to do it, but my question was more about using REGEX. MID( {!YourString}, FIND("{!StartString}", {!YourString}) + LEN("{!StartString}"), FIND("{!...
1 vote

String split on newline creates blank strings for every newline

If you split the string without the brackets, it works properly: String myText = 'Blue\r\n' + 'Green\r\n' + 'Yellow'; List<string> options = myText.split('\r\n');
1 vote

REGEX too complicated in breaking a soap API response

If you want to find the value between two particular flags, I realised recently with the new String methods a few years ago there's one called substringBetween which can be a handy way of grabbing ...
  • 25.1k
1 vote

Pattern.quote matches substring instead of whole word match

The issue is not Pattern.quote, but rather your use of Matcher.find: Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of ...
  • 31.4k
1 vote

Aadhaar card validation rule not working using Regex in salesforce

The Regex Expressions were correct which you have shared in the question but some issues in the implementation . For Pan Card the Validation should be as below. I Just Removed the If() Clause and ...
1 vote

Regex for Email List on Salesforce Validation Rule?

Try with below validation rule separated by comma(,) or semi colon(;). OR( AND(NOT( REGEX( CS_Email_List__c , "([a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z,]{2,4})*") ), NOT( REGEX( CS_Email_List__c , ...

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