Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have something as:-

....wasasa.org.pk
wawwwsasa.msn.com

....ttrt .....ddd.dd www.edu.pk
.ru

I only want to pick following

org.pk
.com
edu.pk

The possible regex i wrote is

[a-z]+.(.*)(?=\s+)

The problem is that its only picking on dd part.

With the latest regex i see following problems:-

....wasasa.org.pk fgf wawwwsasa.msn.com fgf

....ttrt .....ddd.dd www.iffn.pk fgf

www.ru ff www.ru.com fgfgf

.ru fgf

It will stop matching anything after space e.g www.ru.com fgfgs. The regex should work with space and even without space.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Maybe try something like this?

((?:edu|org)?\.[a-z]+)$
share|improve this answer
    
thanks can u rcplain the role of ?: –  asadz Aug 15 '13 at 19:12
    
@asadz Yes, (?: ... ) is a non-capture group. It... will not be captured into a group ^^; –  Jerry Aug 15 '13 at 19:15
    
There is a slight problem if I add ....wasasa.org.pk fgf which is space after .pk it will start to match fgf for me i don't want the match to stop well before that. –  asadz Aug 15 '13 at 19:51
    
@asadz I made a bracketing mistake, oops! Try to see if the updated regex works ^^; –  Jerry Aug 15 '13 at 19:54
    
kindly see my updated question. –  asadz Aug 15 '13 at 20:54

If I did understand the question awk can do it using:

awk '{split($0,uri,"/");c=split(uri[3],domain,".");
domain[1]=="www"?a=3:a=2; for (i=a;i<=c;i+=1){
printf("%s%s"),domain[i],i==c?"\n":"."}}'

A sample usage:

printf "%s\n" 'https://google.com/search?q=google' 'https://www.google.co.uk/foo' | awk '{split($0,uri,"/");c=split(uri[3],domain,"."); domain[1]=="www"?a=3:a=2; for (i=a;i<=c;i+=1){ printf("%s%s"),domain[i],i==c?"\n":"."}}'

Output:

com
co.uk
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.