-2

for example, I have:

String a = abcd http//google.com abcd

and

String b=efgh http//yahoomail.hey.com ihklm

How can I extract only the words:

google
yahoomail.hey
TIA
Ank
  • 1,864
  • 4
  • 31
  • 51

1 Answers1

1

Try this

        string pattern = @"//[a-zA-Z0-9]{1,}[.][a-zA-z]{2,4}";
        string input = @"http//yahoomail.hey.com ihklm";

        foreach (Match m in Regex.Matches(input, pattern))
        {
            Console.WriteLine("'{0}'", m.Value.ToString().Replace("/",""));
        }

Explaination:

// The match needs to start with "//"

[a-zA-Z0-9]{1,} next there can be any amount of characters specified in[ ] 1 to n times

[.] then there needs to be a .

[a-zA-z]{2,4} the domain can be any Upper/lower character and there need to be at least 2 but max 4 characters

to check your Regexes, i suggest using https://regex101.com/
use the standard settingswith PHP flavour. after you are finished creating the Regex, you can use "Code generator" on the left to convert it to JScript/php/python/c#/java/ruby/rust/perl/golang

Jack
  • 60
  • 8
  • why if I tried searching this – DarknessNight Sep 14 '17 at 06:07
  • @Est, This is what [mcve] is for! Every case should be in your test input in your question, with it's expected result. – Drag and Drop Sep 14 '17 at 06:20
  • @DanilEroshenko You could change that, but you dont need to. Est's examples show, that he dont want to have the http:// in front of it. The pattern will match "//yahoomail.hey", the Replace will get "yahoomail.hey" – Jack Sep 14 '17 at 06:34