正则表达式和C# 中的字符串问题

发布于 2024-09-07 10:37:43 字数 1499 浏览 8 评论 0原文

我想拆分字符串 s1 = 6/28/2010 4:46:36 PMs2 = 16:46:36.5013946 。 并将它们连接到新的 s3 = 20010062816463650。但是当我分裂s2时。我的正则表达式不起作用。我现在停了下来。

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConAppTest
{
    class Program
    {

        static void Main(string[] args)
        {
            string s1 = ""; // date
            string s2 = ""; // time
            Console.WriteLine("{0}", DateTime.Now);
            s1 = DateTime.Now.ToString();
            Console.WriteLine("{0}", DateTime.Now.TimeOfDay);
            s2 = DateTime.Now.TimeOfDay.ToString();

            Regex regex1 = new Regex(@" |:|/");    //6/28/2010 2:19:21 PM
            char[] separators1 = { ' ', '/' };
            foreach (string sub1 in regex1.Split(s1))
            {
                Console.WriteLine("Word1: {0}", sub1);
            }

            Regex regex2 = new Regex(@":|.");  //14:19:21.8771215
            char[] separators2 = { ':', '.' };
            foreach (string sub2 in regex2.Split(s2))
            {
                Console.WriteLine("Word2: {0}", sub2);
            }
        }



    }
}
//output
//6/28/2010 4:46:36 PM
//16:46:36.5013946
//Word1: 6
//Word1: 28
//Word1: 2010
//Word1: 4
//Word1: 46
//Word1: 36
//Word1: PM
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:

I want to split my string s1 = 6/28/2010 4:46:36 PM and s2 = 16:46:36.5013946 .
and concatenate them to new s3 = 20010062816463650. But when I split s2. my regex doesn't work. I was paused now.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConAppTest
{
    class Program
    {

        static void Main(string[] args)
        {
            string s1 = ""; // date
            string s2 = ""; // time
            Console.WriteLine("{0}", DateTime.Now);
            s1 = DateTime.Now.ToString();
            Console.WriteLine("{0}", DateTime.Now.TimeOfDay);
            s2 = DateTime.Now.TimeOfDay.ToString();

            Regex regex1 = new Regex(@" |:|/");    //6/28/2010 2:19:21 PM
            char[] separators1 = { ' ', '/' };
            foreach (string sub1 in regex1.Split(s1))
            {
                Console.WriteLine("Word1: {0}", sub1);
            }

            Regex regex2 = new Regex(@":|.");  //14:19:21.8771215
            char[] separators2 = { ':', '.' };
            foreach (string sub2 in regex2.Split(s2))
            {
                Console.WriteLine("Word2: {0}", sub2);
            }
        }



    }
}
//output
//6/28/2010 4:46:36 PM
//16:46:36.5013946
//Word1: 6
//Word1: 28
//Word1: 2010
//Word1: 4
//Word1: 46
//Word1: 36
//Word1: PM
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:
//Word2:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2024-09-14 10:37:43

你为什么要使用 RegEx 来实现这个目的?

使用 DateTime.TryParseDateTime.Parse 将输入字符串解析为一个 DateTime 对象(如果您需要 exact 解析,还有采用格式字符串的 ParseExactTryParseExact )。

然后,您可以将 DateTime.ToString自定义格式一起使用string 将直接从 DateTime 对象输出您想要的确切输出。

DateTime.ToString("yyyyMMddHHmmssff");

Why are you using RegEx at all for this?

Use DateTime.TryParse or DateTime.Parse to parse the input string to a DateTime object (if you need an exact parse, there are also ParseExact and TryParseExact that take a format string).

You can then use DateTime.ToString with a custom format string that will output the exact output you want directly from DateTime objects.

DateTime.ToString("yyyyMMddHHmmssff");
朕就是辣么酷 2024-09-14 10:37:43

点 (.) 在正则表达式中使用时可匹配任何 字符。您需要使用反斜杠对其进行转义:

Regex regex2 = new Regex(@":|\.");

请注意,这与字符串文字中执行的转义完全分开 - 它是正则表达式语言的一部分,而不是 C# 语言。

(我也同意 Oded 的建议,即使用正则表达式可能不是最合适的解决方案。)

A dot (.) matches any character when used in a regular expression. You need to escape it with a backslash:

Regex regex2 = new Regex(@":|\.");

Note that this is entirely separate from the escaping performed in string literals - it's part of the regular expressions language, not the C# language.

(I would also agree with Oded's suggestion that using a regular expression probably isn't the most appropriate solution here anyway.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文