0

Stack!

I am trying to match all text but hyperlinks in .NET. I have found a thread about it, but it is not working for .NET -> Regular expression to match a line that doesn't contain a word?

Exemplifying:

Text text 123 123 timi other text http://www.stack.com asd asd sadokaspodkas

It should match all but http://www.stack.com.

I am trying to lowercase all text, but hyperlinks. I dont have a working regex.

THANKS

Community
  • 1
  • 1
Fernando Silva
  • 292
  • 4
  • 14

1 Answers1

1

You may match a hyperlink, and capture it into Group 1, and then just match any uppercase letter to turn its case to lower. Inside a Regex.Replace method, implement a match evaluator to check if Group 1 matched, and replace accordingly.

Regex.Replace(input, @"(https?://\S+\b)|\p{Lu}", 
                m => m.Groups[1].Success ? 
                   m.Groups[1].Value : m.Value.ToLower()
             )

The (https?://\S+\b) regex is rather rough, there are a lot of URL regex patterns around, choose the one that suits you best.

The \p{Lu} matches a single uppercase letter (including all Unicode uppercase letters). If your links cannot start with HTTP, you may add a + after it (as \p{Lu}+) for better performance.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397