需要将字符串拆分为子字符串但不能使用拆分

时间:2011-10-03 21:08:14

标签: c# asp.net regex

我有一个看起来像这样的字符串:

  

123.45.67.890-1292通过10.98.765.432-4300连接到EDS。

我需要像这样分开它:

   "123.45.67.890-1292 connected to EDS via 10.98.765.432-4300."
    -----+------- --+-              -+-     -----+------- --+-
         |          |                |           |          |
  ClientIP          |      ServiceName      HostIP          |
                    |                                       |
        ClientSession                             HostSession

我正在从具有许多复杂InStr方法的vbscript转换代码。想知道是否有办法使用regEx来做到这一点。

5 个答案:

答案 0 :(得分:8)

(\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+) connected to ([A-Z]+) via (\d{,3}\.\d{,3}\.\d{,3}\.\d{,3})-(\d+)\.

答案 1 :(得分:1)

这是一个匹配/捕获的RegEx:

([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]+)

实现:

        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();

答案 2 :(得分:1)

(?<ClientIP>\d+\.\d+\.\d+\.\d+)-(?<ClientSession>\d+) connected to (?<ServiceName>.*?) via (?<HostIP>\d+\.\d+\.\d+\.\d+)-(?<HostSession>\d+)\.

答案 3 :(得分:1)

因为我不明白为什么要排除String.Split():

var parts = test.Split(new string[] {" connected to ", " via "},
      StringSplitOptions.None);

给你

123.45.67.890-1292
EDS
10.98.765.432-4300

打破-####会话部分需要额外增加1步,也可以使用Split()。

或者更简单:

 var parts = test.Split(' ', '-');

并使用0,1,4,6,7部分

答案 4 :(得分:1)

为什么你不能使用拆分?对单个任务使用正则表达式是不合适的:

([^\-]+)\-(\S+)\s+connected\s+to\s+(\S+)\s+via\s+([^\-]+)\-(\S+)\.

C#代码实现(正则表达式):

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#代码实现(拆分):

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();  
}