0

I've got a string with the format of:

blockh->127.0.0.1 testlocal.de->127.0.0.1 testlocal2.com

Now I need to seperate the elements, best way would be a string array I think. I would need to get only these elements seperated:

127.0.0.5 somerandompage.de

127.0.0.1 anotherrandompage.com

How to split and filter the array to get only these elements?

Using the .Filter() Methode doesn't to the job.

Community
  • 1
  • 1
razoR
  • 19
  • 2

3 Answers3

2

You can use the string Split method:

 var st = "blockh->127.0.0.1 testlocal.de->127.0.0.1 testlocal2.com";
 var result = st.Split(new [] { "->" }, StringSplitOptions.None);

You can achieve the same with a Regex:

var result = Regex.Split(st, "->");

As a note from @Chris both of these will split the string into an array with 3 elements:

  • blockh
  • 127.0.0.1 testlocal.de
  • 127.0.0.1 testlocal2.com

In case you want to get rid of blockh, you can do a regex match using an IP address and domain regex:

 var ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b\s*(([\w][\w\-\.]*)\.)?([\w][\w\-]+)(\.([\w][\w\.]*))");
 var result = ip.Matches(st).Cast<Match>()
        .Select(m => m.Value)
        .ToArray();

This will get only the two elements containing IP addresses.

Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
0

You can use a string Split() method to do that.

var s = "testlocal->testlocal2";
var splitted = s.Split(new[] {"->"}, StringSplitOptions.RemoveEmptyEntries); //result splitted[0]=testlocal, splitted[1]=testlocal2
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
0

If the simpler versions for splitting a string don't work for you, you will likely be served best by defining a Regular Expression and extracting matches.

That is described in detail in this MSDN article: http://msdn.microsoft.com/en-us/library/ms228595.aspx

For more information on how Regular Expressions can look this page is very helpful: http://regexone.com/

Oliver Ulm
  • 531
  • 3
  • 8
  • I don't need to get "blockh". RegEx is overloaded I think, any easier way without RegEx? – razoR Nov 25 '14 at 12:40