KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want a regex that does one thing if it has 3 instances of . in the string, and something else if it has more than 3 instances. for example aaa.bbb.ccc.ddd // one part of the regex aaa.bbb.ccc.ddd.eee // the second part of the regex how do I achieve this in either js or c# ? something like ?(\.){4} then THIS else THAT within the regex... Update Ok basically what I'm doing is this: I want to switch, for any given System.Uri , to another subdomain in an extension method. The problem I came across is that my domains are usually of the form http://subdomain.domain.TLD.TLD/more/url , but sometimes, it can be just http://domain.TLD.TLD/more/url (which just points to www ) So this is what I came up with: public static class UriExtensions { private const string TopLevelDomainRegex = @"(\.[^\.]{2,3}|\.[^\.]{2,3}\.[^\.]{2,3})$"; private const string UnspecifiedSubdomainRegex = @"^((http[s]?|ftp):\/\/)(()([^:\/\s]+))(:([^\/]*))?((?:\/)?|(?:\/)(((\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?))?$"; private const string SpecifiedSubdomainRegex = @"^((http[s]?|ftp):\/\/)(([^.:\/\s]*)[\.]([^:\/\s]+))(:([^\/]*))?((?:\/)?|(?:\/)(((\w+)*\/)([\w\-\.]+[^#?\s]+)(\?([^#]*))?(#(.*))?))?$"; public static string AbsolutePathToSubdomain(this Uri uri, string subdomain) { subdomain = subdomain == "www" ? string.Empty : string.Concat(subdomain, "."); var replacement = "$1{0}$5$6".FormatWith(subdomain); var spec = Regex.Replace(uri.Authority, TopLevelDomainRegex, string.Empty).Distinct().Count(c => c == '.') != 0; return Regex.Replace(uri.AbsoluteUri, spec ? SpecifiedSubdomainRegex : UnspecifiedSubdomainRegex, replacement); } } Basically with this code I take the Sys
Tags (comma-separated)
Save Edits
Cancel