0

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());
} 
5
  • What errors are you getting in Salesforce?
    – Mark Pond
    Jan 4, 2016 at 16:58
  • @MarkPond couple of error I am getting like Illegal character sequence '\/' in string literal. If I remove all the `\` then I get another error like reg exp not valid
    – Ratan Paul
    Jan 4, 2016 at 17:03
  • did you try \\/ as I believe you have to escape the escape...
    – Eric
    Jan 4, 2016 at 17:27
  • no @Eric let me try that
    – Ratan Paul
    Jan 4, 2016 at 17:27
  • 1
    Can I ask why this question got downvote?
    – Ratan Paul
    Jan 5, 2016 at 2:58

2 Answers 2

2

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());
}
0

You need to escape your backslashes. So anywhere you have \ needs to become \\.

(?:(?:(?:[a-z]{3,9}:(?:\\\\/\\\\/)?)(?:[-;:&=+$,\\\\w]+@)?[a-z0-9.-]+|(?:www\\\\.|[-;:&=+$\u200c\u200b,\\\\w]+@)[a-z0-9.-]+)((?:\\\\/[+~%\\\\/.\\\\w-]*)?\\\\??(?:[-+=&;%@.\\\\w]*)#?\\\\w*)?)
3
  • If I use this regex. Then my result don't return any URL's
    – Ratan Paul
    Jan 4, 2016 at 17:33
  • Maybe you should try a simpler pattern. Your question is really about how to resolve the illegal character error, not how to make a regex that suits your needs. That would probably be more appropriate on Stack Overflow anyway.
    – Adrian Larson
    Jan 4, 2016 at 17:34
  • 1
    what is the use if I modify expression. If that won't work as regexr.com/3cgrq then what is the use
    – Ratan Paul
    Jan 4, 2016 at 17:46

You must log in to answer this question.

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