[SalesForce] Problem with get all the link from String Regular Expression

Below is the regex I tried to get all the urls from string:

(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[a-z0-9.-]+|(?:www\.|[-;:&=+$‌​,\w]+@)[a-z0-9.-]+)((?:\/[+~%\/.\w-]*)?\??(?:[-+=&;%@.\w]*)#?\w*)?)

It is working fine here, check this out http://regexr.com/3cgrq

but if I use pattern matching in salesforce get multiple error.

Illegal character sequence '/' in string literal.

If I remove all the \ then I get another error

System.StringException: Invalid regex: Dangling meta character '?'

Pls try to execute below code in developer console

String myvar = 'raewaferwww.dilyan.co.ukhrewadfea'+
''+
'<span style="color: #5f604b;">'+
'<span style="font: signacondcolumn-book;">'+
'<span style="font-size: 8.0pt;">https://www.merchant.com/store</span>'+
'</span>'+
'</span>'+
'<br>'+
'<a href\'="www.account.com">Account</a>';

// First, instantiate a new Pattern object "MyPattern"
Pattern MyPattern = Pattern.compile('(?:(?:(?:[a-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[a-z0-9.-]+|(?:www\.|[-;:&=+$‌​,\w]+@)[a-z0-9.-]+)((?:\/[+~%\/.\w-]*)?\??(?:[-+=&;%@.\w]*)#?\w*)?)');

// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher(myvar);

while (MyMatcher.find()) { 
    System.debug(MyMatcher.group());
} 

Best Answer

Ok after no of changes. I finally able to get the correct expression

(?:(?:(?:[a-z]{3,9}:(?://)?)(?:[-;:&=+$,w]+@)?[a-z0-9.-]+|(?:www.|[-;:&=+$??,w]+@)[a-z0-9.-]+)((?:/[+~%/.w-]*)?\\??(?:[-+=&;%@.w]*)#?w*)?)

This will return all the links from String

enter image description here


Code

String myvar = 'raewaferwww.dilyan.co.ukhrewadfea'+
''+
'<span style="color: #5f604b;">'+
'<span style="font: signacondcolumn-book;">'+
'<span style="font-size: 8.0pt;">https://www.merchant.com/store</span>'+
'</span>'+
'</span>'+
'<br>'+
'<a href\'="www.account.com">Account</a>';


String myvar1 = '(?:(?:(?:[a-z]{3,9}:(?://)?)(?:[-;:&=+$,w]+@)?[a-z0-9.-]+|(?:www.|[-;:&=+$??,w]+@)[a-z0-9.-]+)((?:/[+~%/.w-]*)?\\??(?:[-+=&;%@.w]*)#?w*)?)';

// First, instantiate a new Pattern object "MyPattern"
Pattern MyPattern = Pattern.compile(myvar1);

// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher(myvar);

while (MyMatcher.find()) { 
    System.debug(MyMatcher.group());
}
Related Topic