How do I include a hyphen in a Regex hyperlink?

I try to find links in the text entered by the user and automatically convert them to a link.

I use the current Regex as the following, which is useful for finding hyperlinks from text.

Regex regexResolveUrl = new Regex("((http://|www\\.)([A-Z0-9.-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);

It works well for almost all links while I stumbled, but this is a problem when I want to detect links with hypen.

i.e. www.abc-xyz.com will not work using regex, can anyone help me with this?

+3
source share
3 answers

Reset the hyphen:

 Regex("((http://|www\\.)([A-Z0-9.\-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
+2
source

, - , ( ) . , [abc-] - , 4 , a, b, c, -. , [ab-c] 3 , -, - - .

, - ( ):

[A-Z0-9.-:]

3 : a Z 0 9 . (ASCII 46) : (ASCII 58). :

[A-Z0-9.:-]


, {1,} " ".

.NET regex ( ) :

  • ?: "zero-or-one" {0,1}
  • *: "zero-or-more" {0,}
  • +: " " {1,}

, .


# @ -

, . Java ( ), # @ - .

:

"(http://|www\\.)"
@"(http://|www\.)"

"c:\\Docs\\Source\\a.txt"
@"c:\Docs\Source\a.txt"

@ , (, , ).

+8

Add a hyphen as the first or last character in the character class.

+2
source

Source: https://habr.com/ru/post/1749819/


All Articles