tags:

views:

115

answers:

2

I've written a regular expression that automatically detects URLs in free text that users enter. This is not such a simple task as it may seem at first. Jeff Atwood writes about it in his post.

His regular expression works, but needs extra code after detection is done.

I've managed to write a regular expression that does everything in a single go. This is how it looks like (I've broken it down into separate lines to make it more understandable what it does):

1   (?<outer>\()?
2   (?<scheme>http(?<secure>s)?://)?
3   (?<url>
4       (?(scheme)
5           (?:www\.)?
6           |
7           www\.
8       )
9       [a-z0-9]
10      (?(outer)
11          [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+(?=\))
12          |
13          [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+
14      )
15  )
16  (?<ending>(?(outer)\)))

As you may see, I'm using named capture groups (used later in Regex.Replace()) and I've also included some local characters (čšžćđ), that allow our localised URLs to be parsed as well. You can easily omit them if you'd like.

Anyway. Here's what it does (referring to line numbers):

  • 1 - detects if URL starts with open braces (is contained inside braces) and stores it in "outer" named capture group
  • 2 - checks if it starts with URL scheme also detecting whether scheme is SSL or not
  • 3 - start parsing URL itself (will store it in "url" named capture group)
  • 4-8 - if statement that says: if "sheme" was present then www. part is optional, otherwise mandatory for a string to be a link (so this regular expression detects all strings that start with either http or www)
  • 9 - first character after http:// or www. should be either a letter or a number (this can be extended if you'd like to cover even more links, but I've decided not to because I can't think of a link that would start with some obscure character)
  • 10-14 - if statement that says: if "outer" (braces) was present capture everything up to the last closing braces otherwise capture all
  • 15 - closes the named capture group for URL
  • 16 - if open braces were present, capture closing braces as well and store it in "ending" named capture group

First and last line used to have \s* in them as well, so user could also write open braces and put a space inside before pasting link.

Anyway. My code that does link replacement with actual anchor HTML elements looks exactly like this:

value = Regex.Replace(
    value,
    @"(?<outer>\()?(?<scheme>http(?<secure>s)?://)?(?<url>(?(scheme)(?:www\.)?|www\.)[a-z0-9](?(outer)[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+(?=\))|[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+))(?<ending>(?(outer)\)))",
    "${outer}<a href=\"http${secure}://${url}\">http${secure}://${url}</a>${ending}",
    RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

As you can see I'm using named capture groups to replace link with an Anchor tag:

"${outer}<a href=\"http${secure}://${url}\">http${secure}://${url}</a>${ending}"

I could as well omit the http(s) part in anchor display to make links look friendlier, but for now I decided not to.

Question

I would like my links to be replaced with shortenings as well. So when user copies a very long link (for instance if they would copy a link from google maps that usually generates long links) I would like to shorten the visible part of the anchor tag. Link would work, but visible part of an anchor tag would be shortened to some number of characters. I could as well append ellipsis at the end of at all possible (and make things even more perfect).

Does Regex.Replace() method support replacement notations so that I can still use a single call? Something similar as string.Format() method does when you'd like to format values in string format (decimals, dates etc...).

+1  A: 

You would have to use the Regex.Replace overload that uses a MatchEvaluator, a delegate that constructs the replacement text for you.

See here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx

Technically, it is possible with just regexes, by doing what Kobi suggests. I'm not sure I'd want to ask anybody (including yourself after a few months) to maintain that regex however.

Thorarin
+1  A: 

You can split ${url} to two capturing groups - urlhead, with the number of characters you want to display, and urltail with the rest. Here's an example with 10 characters; this is somewhat simplfied to remove the condition, the last (?<ending>(?(outer)(?=\)))) should take care of that - it backtracks and captures the last ) when needed:

(?<outer>(?<=\())?
(?<scheme>http(?<secure>s)?://)?
(?<url>
    (?(scheme)
        (?:www\.)?
        |
        www\.
    )
    [a-z0-9]
    [-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]{1,10}
)
(?<urltail>[-a-z0-9/+&@#/%?=~_()|!:,.;čšžćđ]+)
(?<ending>(?(outer)(?=\))))

Note that I've also changes outer and ending to be lookarounds, so they are not captured and replaced. The replace string in this case looks like:

<a href=\"http${secure}://${url}${urltail}\">http${secure}://${url}</a>
Kobi
That would work, but imo it's obfuscated enough as it is :)
Thorarin
Actually, looking at it again, it isn't so scary; you just have to add the `?(outer)` part again. Looks like it's well documented, too.
Kobi
@Kobi: You're right, but regex would probably become really complicated because it would have lots of conditions. It should however capture all: the shorter ones, the longer ones, each enclosed in braces or not etc. I will think about it, but I'm not really sure if that's an optimal solution.
Robert Koritnik
@Kobi: What do you mean by "you just have to add the `?(outer)` part again"? Where to?
Robert Koritnik
@Robert - I'm having a little trouble with edge cases so I can't post the full solution, but the idea is to limit `?<url>` to a fixed maximal number of characters. A simplified example: instead of `(.+)`, use `(.{1,10})(.*)` - this will allow an easy replace, showing only the first 10 characters.
Kobi
@Kobi: I think a better way would be to use a lookahead before capturing the "url". A lookahead could determine url length and its result could be used in an if statement that would either capture url or url + reminder. Maybe that would be a way to go.
Robert Koritnik
@Robert - Very possibly, but then, how will you build the full url in the link? Does a lookahead capture groups?
Kobi
link would always consist of both parts even though there would be no reminder, but if it was, everything would be fine. link display would always display just url.
Robert Koritnik
@Kobi: Lookarounds don't capture anything. They're just used to look around and give you some info for capturing.
Robert Koritnik
@Robert - see the updates, this is handled with a single `replace`, ans simplified your regex.
Kobi
Hat's down @Kobi. This is actually working as it should! Good work. I haven't tried yet, so this will come in handy when I'll actually use it. Thanks a bunch. I upvoted you as well as accepted your answer since you delved so deeply into it. But I'm sure this common effort of ours will benefit someone else as well. ;)
Robert Koritnik