Solved

C# REgExp Extract domain only

Posted on 2013-01-13
2
305 Views
Last Modified: 2013-01-13
Hi people, I am trying to get an regexp working.

I want to extract the domain by searching in an text using regexp.

Example:

---1---
http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
>>> Extracting = http (protocol)
>>> Extracting = derekslager.com


---2---
http://pew.no-ip.biz/layouts/15/start.aspx#/anyingstrangelink
>>> Extracting = http (protocol)
>>> Extracting = pew.no-ip.biz

---3---
http://cdn.http.pri.streamotor.com/1/2/video-sd.mp4?Expires=1350310145&Key-Pair-Id=APKAJUAT6SMTUDSHTR3Q&Signature=fQ2bVVTZDdtoaFsc41cnR0GgoA2Y
>>> Extracting = http (protocol)
>>> Extracting = cdn.http.pri.streamotor.com

---4---
http://www.google.com (www prefix oO)
>>> Extracting = http (protocol)
>>> Extracting = google.com
---4---

I tried to achive using this but failing on example 2 :(, havent tried 3 and 4

            var regex = new Regex(@"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?");

Open in new window


Thaks
0
Comment
Question by:chugarah
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38772381
I'd say start with this:

var regex = new Regex("(?<=(file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)[^/]+");

Open in new window


...and then write rules in C# code to determine what you want to filter out of the host value that is returned. It will be much easier to write, read, and understand.
0
 
LVL 1

Author Closing Comment

by:chugarah
ID: 38773504
Thanks, a good start
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS subreport with parameter 3 48
Reference issues in Visual Studio 3 41
how to double quote a string for an inline sql statement. 8 73
itextsharp with c# 3 9
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question