Solved

C# REgExp Extract domain only

Posted on 2013-01-13
2
299 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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 24
Windows Service with UDP 2 28
Expando 4 36
bulid json format 3 21
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now