Solved

C# REgExp Extract domain only

Posted on 2013-01-13
2
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array not updating 8 42
How to set focus on a dynamic control 18 52
Concurrency Error Persistent using Data Grid View Update C# 2 29
How does this modal gets closed? 6 41
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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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