Quantcast
Channel: Pattern.Split doesn't behave as I've expected - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 3

Pattern.Split doesn't behave as I've expected

$
0
0

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);System.debug(urls);

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)?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images