4

I have a regular expression in apex that is only grabbing part of the link I need in a string. I need it to grab the entire link.

Here is what im working with:

String myvar = 'this is an example http://test.com/testing/123654123';

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

            Pattern MyPattern = Pattern.compile(myvar1);

            Matcher MyMatcher = MyPattern.matcher(myvar);

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

This is only returning http://test.com/

How can I modify the regular expression to provide the complete link?

1

You might try a few of the regexps mentioned in the article Detecting URLs in a Block of Text but the article also nails it by saying

The short answer is that it can’t be done. Pretty much any character is valid in URLs.

0

String myvar = 'this is an example http://test.com/testing/123654123';

String myvar1 = 'http'+myvar.split('http')[1];

System.debug(myvar1);

2
  • This doesn't work for me :( I didn't provide the exact string in the example but it has alot of %'s around it Here is a better example of what myvar = looks like String myvar = 'this is an example test.com/testing/123654123%0A%0A%0A%0A2.%20%20'; This is why I need to modify my regular expression – Niche Apr 14 '16 at 2:34
  • Any ideas? I really need to just modify im existing regex string – Niche Apr 14 '16 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.