正则表达式和C# 中的字符串问题
我想拆分字符串 s1 = 6/28/2010 4:46:36 PM
和 s2 = 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你为什么要使用
RegEx
来实现这个目的?使用
DateTime.TryParse
或DateTime.Parse
将输入字符串解析为一个DateTime
对象(如果您需要 exact 解析,还有采用格式字符串的ParseExact
和TryParseExact
)。然后,您可以将
DateTime.ToString
与 自定义格式一起使用string 将直接从DateTime
对象输出您想要的确切输出。Why are you using
RegEx
at all for this?Use
DateTime.TryParse
orDateTime.Parse
to parse the input string to aDateTime
object (if you need an exact parse, there are alsoParseExact
andTryParseExact
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 fromDateTime
objects.点 (
.
) 在正则表达式中使用时可匹配任何 字符。您需要使用反斜杠对其进行转义:请注意,这与字符串文字中执行的转义完全分开 - 它是正则表达式语言的一部分,而不是 C# 语言。
(我也同意 Oded 的建议,即使用正则表达式可能不是最合适的解决方案。)
A dot (
.
) matches any character when used in a regular expression. You need to escape it with a backslash: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.)