-5

i will be using the expression with

Regex.Replace();

to replace the rest with "".

inputs: http://therealzenstar.blogspot.fr output: blogspot.fr

wss
  • 1
  • 4
  • 1
    Your question is really, really vague. Edit it to add some more detail - what are the inputs? What is the expected output? What have you tried so far? – Chris Mantle Jul 09 '15 at 08:19
  • Welcome to StackOverflow. Please be aware that the quality of your question stronlgy relates to how good people can help you. There is too little information in your question (yet) to generate any meaningful answers. Please edt it with some more details. Have a look at: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Jens H Jul 09 '15 at 08:25
  • `/https?:\/\/\w+\.(\w+\.\w+)/` this can work for your inputs. – Kerwin Jul 09 '15 at 08:45

1 Answers1

0

Just to iterate on Jens' comment, we have to guess: What is your expected output when additional information appears, e.g. http://therealzenstar.blogspot.fr/somedata.html. Is it still blogspot.fr? Are such examples needed to be adresed?

You said you want to replace "everything else" with "". Replace() will replace everything that is matched with what you want. So, to replace it with "", you'd need to match everything that you do not want.It's possible, however, it's much easier to capture what you DO want and replace all the match with $1.

Assuming you always want only the domain.xx, even if more information appears. Something like this will work: ^(?:https?:\/\/)?[^\/\s]*\.([^.\s\/]*\.[^.\s\/]*)(?:$|\/.*), as seen: https://regex101.com/r/hN8iQ7/1

A problem arises if your domains also include those with multiple extensions. I.e. domain.co.uk. You'd need to adress them specifically (naming them), as it is very hard to generalize a way to distinguish these items.

^(?:https?:\/\/)?[^\/\s]*?\.([^.\s\/]*\.(?:co\.uk|[^.\s\/]*))(?:$|\/.*) - with .co.uk option added. https://regex101.com/r/hN8iQ7/2 .

yourregex.Replace(yourstring, "$1") may do what you need.

Andris Leduskrasts
  • 1,170
  • 6
  • 16