Need to split a string into substrings but can't use split
I have a string that looks like this:
123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.
I need to split it like so:
"123.45.67.890-1292 connected to EDS via 10.98.765.432-4300."
-----+------- --+- -+-开发者_JAVA技巧 -----+------- --+-
| | | | |
ClientIP | ServiceName HostIP |
| |
ClientSession HostSession
I'm converting the code from vbscript that has a lot of complex InStr methods. Was wondering if there was a way to do this using a regEx.
(\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+) connected to ([A-Z]+) via (\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+)\.
Why can't you use split? Using regular expression for single task is inappropriate:
([^\-]+)\-(\S+)\s+connected\s+to\s+(\S+)\s+via\s+([^\-]+)\-(\S+)\.
C# code implementation (regular expression):
static void Main(string[] args)
{
String input = "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.";
String pattern = @"([^\-]+)\-(\S+)\s+connected\s+to\s+(\S+)\s+via\s+([^\-]+)\-(\S+)\.";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
foreach (var group in match.Groups)
{
Console.WriteLine(group);
}
}
Console.ReadKey();
}
C# code implementation (splitting):
public class DTO
{
public string ClientIP { get; set; }
public string ClientSession { get; set; }
public string ServiceName { get; set; }
public string HostIP { get; set; }
public string HostSession { get; set; }
}
static void Main(string[] args)
{
String input = "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.";
String[] splits = input.Split(new char[] { ' ' });
DTO obj = new DTO();
for (int i = 0; i < splits.Length; ++i)
{
switch (i)
{
// connected
case 1:
// to
case 2:
// via
case 4:
{
break;
}
// 123.45.67.890-1292
case 0:
{
obj.ClientIP = splits[i].Split(new char[] { '-' })[0];
obj.ClientSession = splits[i].Split(new char[] { '-' })[1];
break;
}
// EDS
case 3:
{
obj.ServiceName = splits[i];
break;
}
// 10.98.765.432-4300.
case 5:
{
obj.HostIP = splits[i].Split(new char[] { '-' })[0];
obj.HostSession = splits[i].Split(new char[] { '-' })[1];
break;
}
}
}
Console.ReadKey();
}
(?<ClientIP>\d+\.\d+\.\d+\.\d+)-(?<ClientSession>\d+) connected to (?<ServiceName>.*?) via (?<HostIP>\d+\.\d+\.\d+\.\d+)-(?<HostSession>\d+)\.
Here's a RegExp to match/capture that:
([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+) connected to ([a-zA-Z]+) via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)
implementation:
string pat = @"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+) connected to ([a-zA-Z]+) via ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)-([0-9]+)";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match match = r.Match("123.45.67.890-1292 connected to EDS via 10.98.765.432-4300.");
foreach (var str in match.Groups)
Console.WriteLine(str);
Console.ReadKey();
Since I don't see why you rule out String.Split() :
var parts = test.Split(new string[] {" connected to ", " via "},
StringSplitOptions.None);
gives you
123.45.67.890-1292
EDS
10.98.765.432-4300
breaking of the -####
session parts would take 1 extra step, also possible with Split().
Or maybe easier:
var parts = test.Split(' ', '-');
and use parts 0, 1, 4, 6, 7
精彩评论