A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Hot answers tagged

9

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 underscore character (_). Here's the expression I would use: (_|[^\w\s]) What does it mean? 1st Capturing Group (_|[^\w\s]) Matches either alternative 1st ...


7

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 String: String s = '~!@#$%^*()_+|}{":?><`=;/.,][-\'\\'; System.debug(s.replaceAll('\\p{Punct}','')); Note that the "escapes" are not disappearing, they're ...


6

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 modifier flag to get this behavior. Many languages use m to mean multi-line: The ^ and $ anchors now match at the beginning/end of each line respectively, ...


5

matches matches the entire string against the Pattern (by default; you'll want to read more about Regions in the documentation). Instead, you'll want to use find: Matcher matcher=Pattern.compile(regex).matcher(str); Boolean result= matcher.find(); To read the matched string, use the zeroth group: System.debug('Matching String: '+matcher.group(0));


4

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 methods: find() Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the ...


4

The for loop would be more efficient (e.g. stops at first match). The only way to avoid a for loop would be to sacrifice performance: Map<Boolean, String> theMap = new Map<Boolean, String>() { pattern_a.matcher(someValue).matches() => model_a, pattern_b.matcher(someValue).matches() => model_b, pattern_c.matcher(someValue).matches() =&...


4

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 introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the ...


3

You have to call myMatcher.find(). Calling matches just tells you if the expression matches the whole input. Also note that you need to escape your special characters (\\b) and you probably want to search only up to the end of the line ($). That will only work if you set the multi-line flag, otherwise $ means end of string. Matcher m = Pattern.compile('(?m)^...


3

The pattern, while technically more efficient, suffers from the 1,000,000 iteration limit, outlined in 000005024: Why do we get System.Exception: Regex too complicated error?. In other words, if bodyString.length() * words.size() exceeds 1,000,000, you need to use contains. Otherwise, you can use the pattern. As a matter of practice, you could even use both ...


3

I was able to sort this out just now. Unfortunately, the error that the Force.com IDE gives is not terribly obvious, and the problem appears to be bug in the underlying Java interpreter (compiler? I forget). Apex really doesn't like curly-brackets in strings. Double escaping them solves the problem! pattern myPattern = pattern.compile('^(?:.*)(?:\\{"id"...


3

You can use generate a List<String> to store results using a while loop. Also, change your pattern to ("[^"]*"). The following worked for me in Execute Anonymous: static final Pattern doubleQuoteWrapped = Pattern.compile('("[^"]*")'); static List<String> parse(String input) { Matcher m = doubleQuoteWrapped.matcher(input); List<String&...


2

Assuming that your regex is correct you can do the following to iterate over the matches: Pattern p = Pattern.compile(yourRegex); Matcher pm = p.matcher(yourString); if (pm.matches()) { for (Integer i = 1; i <= pm.groupCount(); i++) { System.debug(pm.group(i)); } } Reference: Matcher Class


2

Try with this: String regEx = 'Main-[a-zA-Z0-9- ]*-Accepted'; Copy and Paste Code: String regEx = 'Main-[a-zA-Z0-9- ]*-Accepted'; System.debug(' @@@@@@ Main-NOT SUBMITTED-Accepted '+Pattern.matches(regEx,'Main-NOT SUBMITTED-Accepted')); System.debug(' @@@@@@ Main-NOT-SUBMITTED-Accepted '+Pattern.matches(regEx,'Main-NOT-SUBMITTED-Accepted')); System.debug('...


2

Looking at your string like this: 14-Mar-16,RTRDTG78BGXPBE3H,D-Link DIR-600M Wireless N150 Home Router, 10-Apr-16,SECEBH7HZMKZMG3B,Kaspersky Internet Security 2016 1 PC 1 Year it appears to be a repeating pattern of 3 values separated by commas. If that is the case, personally I would just go for: String[] parts = '14-Mar-16,RTR...Year'.split(','); and ...


2

Regular expressions work on regular languages, but HTML is not a regular language. So, while you can do some limited extraction of html using a regexp, regexps are not the right tool for the job. Instead, I'd suggest using the Dom.Document class and the Dom.XmlNode class A brief example to get you going Dom.Document inDoc = new Dom.Document(); inDoc.load(...


2

As per Salesforce documentation, there is a function called unescapeJava() available in String class which returns a String whose Java literals are unescaped. Literals unescaped include escape sequences for quotes (\") and control characters, such as tab (\t), and carriage return (\n). I hope this can solve your issue.


2

Depending on the size of your payload, you might run out of heap space while parsing the string via regex. If your payload is stable enough, this alternative method using the JSON serializer might allow you to fetch the User Id from the payload and ignore the rest as well as make it possible to quickly access attributes other than id without making your ...


2

I don't think you can add RecordType in the Matching Rule Criteria based on this feature request in Ideas: Include Record Type as a field option for Matching Rules. When setting up Matching Rules, there are times when you may want to allow duplicates values in a field if the record types don’t also match. To do this it would be helpful if we could ...


2

Try this _[^_c] This searches for _ that is not followed by _c Try it here: https://regex101.com/ You can test it this way: String test1 = 'Opportunity__c'; String test2 = 'Opportunity_abc__c'; String regex = '_[^_c]'; System.debug(test1.split(regex).size() != 1); // false System.debug(test2.split(regex).size() != 1); // true This may be a bit wierd, ...


2

My favorite site to help with RegEx things like this is GSkinner's RegEx site where you can test these patterns and it also has a lot of help. What you're going to want to do with this is is create a group to capture the digits as optional followed by another optional group to catch the alpha character, again as optional, then a group to capture the street ...


1

Here's a service (BarService) with an overloaded method execute. In both methods, true is returned. public class BarService { public Boolean execute(Date d) {return true;} public Boolean execute(Date d, Id uId) {return true;} } And here's another service (the code-under-test, named FooService with method-under-test doStuff()) that calls both ...


1

You need to build Custom Report Types for Duplicate Record Reports. Use the Report action to fine-tune your duplicate rules. Duplicate record reports let you analyze the quality of your data and see how well your duplicate rules are working. Use the examples we provide to set up the appropriate custom report types. The following records appear in these ...


1

One problem with contains or containsIgnoreCase, is that you can still hit a match on a subset string. For example, searching for LOW would hit any word that contains the string, like HOLLOW, BELOW, BLOW, LOWER, BLOWER, etc.


1

I was able to use the following pattern: (?si)<h3[^>]+?>(.*)<\/h3> Explanation: (?si) The s flag indicates that . should match newline characters. Might as well use the i flag to make it case insensitive. <h3[^>]+?> Match the opening tag. (.*) Match all characters as match group 1. <\/h3> Match the closing tag. Here ...


1

Honestly, you shouldn't be trying to parse HTML yourself. Just load up a Document, then recursively walk through the XmlNode elements, grabbing whatever attributes/text you want. This avoids an entire class of problems associated with using patterns (complexity limits, etc) when all you want is straightforward access to a fragment of XML-style data.


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