Full doc on Pattern Class: Documentation Pattern Class

Use case :

So, whenever a Task is Created due to email to salesforce, I need to check if that emails content Body included one or more links from a list I store on a static Resource. My problem comes when I tried to build a way to check all the links in the body of the email I wanted to use some regular expression so I could learn more about them.

I realized that for a really basic and inefficient regular expression for detecting links like : 'http.*\\S'

When I started looking into the System.Pattern Class I realized the function Split(String regex) had a different description that should match my requirements unlike String.Split()

String Class:

public String[] split(String regExp) 

Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.

Pattern Class :

public String[] split(String regExp)

Returns a list that contains each substring of the String that matches this pattern.

My problem:

From what I understand here string class returns all the strings that are separated by the substrings that met the regular expression. The pattern class tho describes it as if the returned list of strings are all the substrings that met the regular expression, but in first place, should then the name of the parameter be another that's not regExp, sounds really confusing to me, since I've compiled my regex and using the Pattern returned to call this that's actually the regex i'm using and not the parameter, the parameter should be the string i'm trying to split.

Regex test

So far the test seems to work fine on this website. But when I use this compiled regular expression on this Annonymous code :

System.Pattern regularExpression = System.Pattern.compile('http.*\\S');
string s = 'Test bla bla bla \r http://test.xxxx.com/labs/test/files/unrelateddoc.pdf';
List <string> urls = regularExpression.split(s);

The result is unexpected :

enter image description here

Am I missing something trivial which changes the whole picture(Could be since my regex experience started yesterday)?

2 Answers 2


You do indeed have a misunderstanding here. What the tool you're using does is find each matching group, which is more like this script instead:

Pattern urlPattern = Pattern.compile('http.*\\S');
String input = 'Test bla bla bla \r http://test.xxxx.com/labs/test/files/unrelateddoc.pdf';
Matcher m = urlPattern.matcher(input);
while (m.find()) system.debug(m.group());

Your regex(http.*\\S') will match anything that starts with http and it will match till the end of line(combination of .* and \S).

.* will match ANY character and \S will match any non-space character(see this answer)

This means that your string will be split into what is before the regex match( **Test bla bla bla \r **) and what is after your regex match(nothing). That's why you're getting 2 strings in your output and second one is empty

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .