0

I have a regex that I've been using for a while to highlight a hashtag (eg. #stackoverflow) or a username/mention (eg. @jeffjohnson) while a user is typing out text.

Here is how I create that regex:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|@)(\\w+)|(#|@)" options:0 error:&error];

I want to add to this regex pattern so that it will also highlight a URL. So I changed it to the pattern to the following: @"(#|@|http)(\\w+)|(#|@|http)"

This works correctly when they type http it will become highlighted but when they type a colon (:) the colon is not highlighted.

If I try adding www to the regex pattern the same way that I added http, the same thing happens. www will highlight correctly but as soon as you type a period (.) the period will not highlight.

How can I add full URL detection to this regex pattern?

user3344977
  • 3,454
  • 4
  • 29
  • 82

3 Answers3

1

This is because your regular expression is looking for (in English): A hash OR an At symbol OR http immediately followed by the character class \w (which evaluates to [a-zA-Z0-9_] which is a character class that allows any letter regardless of case, any digit, as well as an underscore.) and therein lies your problem, if you want to highlight a URL as well as hash tags and username/mentions I would recommend a regex like this:

((?:#|@)\w+|http\S+)

if you want, you can get as complex as you wish in your regular expression but this would work just fine for most cases.

In English, this regular expression is looking for: Either a hash OR an at symbol followed by the character class \w, OR http followed by one or more non-space characters.

Pete Nixon
  • 11
  • 4
1

For URLs you should not try to roll your own regex. The format of URLs is extremely loose. The W3C does provide a standard reference regex in an RFC (I forget which one)

What you should use in this case is the built in NSDataDetector class which has already done the work for you for finding what it calls links.

uchuugaka
  • 12,378
  • 6
  • 33
  • 54
0

Use this regex, I have not tested it but I am sure it is almost correct you may need to modify a bit in it otherwise I think it should work

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|@|((http|https)[:][/][/](w{3}[.])?([a-z]+[.])+([a-z]+[/]*)))" options:0 error:&error];

It will accept URLs also like this URL

http://stackoverflow.com/questions/32211615/regex-works-perfectly-for-hashtags-and-usernames-but-not-for-urls/32212039#32212039

and of-course simple one like

http://google.com
jscs
  • 62,161
  • 12
  • 145
  • 186
Muhammed Irfan
  • 1,472
  • 11
  • 25