正则表达式确保使用 \r\n

发布于 2024-12-01 07:09:08 字数 186 浏览 1 评论 0原文

我正在寻找一个正则表达式来确保我的字符串仅包含换行符 \r\n 而不是 \r 或 \n。

无错误的示例文本:
您好,\r\n\r\n欢迎搭乘我们全新的邮轮。\r\n亲切的问候

有错误的示例文本:
您好\r\r\n欢迎搭乘我们全新的游轮航线。\n谨致问候,

提前感谢您!
亲切的问候,丹尼

I'm looking for a regex to ensure that my string only contains \r\n for newline and not \r or \n.

Sample text without errors:
Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards

Sample text with errors:
Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards

Thank you in advance!
Kind regards, Danny

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

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

发布评论

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

评论(2

违心° 2024-12-08 07:09:08

尝试这个正则表达式:

(\r[^\n])|([^\r]\n)

如果它匹配,您的文本包含松散的 \r\n
注意:如果将其放入字符串中,则需要转义 \r\n 两次。或者更好地将它放在 @ 字符串中,如下所示 @"(\r[^\n])|([^\r]\n)"

编辑:示例:

using System;
using System.Text.RegularExpressions;
public class Test
{
        public static void Main()
        {
                Regex r = new Regex(@"(\r[^\n])|([^\r]\n)");
                string[] Test = { "Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards", 
                                         "Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" };

                foreach(string t in Test)
                {
                        System.Console.Write("\"{0}\" ", t.Replace("\r", "\\r").Replace("\n", "\\n"));
                        if(r.IsMatch(t))
                                System.Console.WriteLine("Is not ok");
                        else
                                System.Console.WriteLine("Is ok");
                }
        }
}

输出:

"Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards" Is ok
"Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" Is not ok

链接:http://ideone.com/mauNN

Try this regex:

(\r[^\n])|([^\r]\n)

if it matches, your text contains a loose \r or \n
Note: if you put this in string you need to escape the \r\n twice. or even better put it in @ string like this @"(\r[^\n])|([^\r]\n)"

Edit: example:

using System;
using System.Text.RegularExpressions;
public class Test
{
        public static void Main()
        {
                Regex r = new Regex(@"(\r[^\n])|([^\r]\n)");
                string[] Test = { "Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards", 
                                         "Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" };

                foreach(string t in Test)
                {
                        System.Console.Write("\"{0}\" ", t.Replace("\r", "\\r").Replace("\n", "\\n"));
                        if(r.IsMatch(t))
                                System.Console.WriteLine("Is not ok");
                        else
                                System.Console.WriteLine("Is ok");
                }
        }
}

Output:

"Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards" Is ok
"Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" Is not ok

Link: http://ideone.com/mauNN

摘星┃星的人 2024-12-08 07:09:08

我修复了上一个示例中的错误,这是一个测试工作示例,

using System;
using System.Text.RegularExpressions;
public class Test
{
        public static void Main()
        {
        Regex rxSingleCharNewLine = new Regex(@"\r(?!\n)|(?<!\r)\n",RegexOptions.Singleline);
        Regex rxNewLine = new Regex(@"\r\n",RegexOptions.Singleline);
                string[] Test = { "Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards", 
                                         "Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" };

                foreach(string t in Test)
                {
                        System.Console.Write("\"{0}\" ", t.Replace("\r", "\\r").Replace("\n", "\\n"));
                        if(!rxSingleCharNewLine.IsMatch(t) && rxNewLine.IsMatch(t))
                                System.Console.WriteLine("Is ok");
                        else
                                System.Console.WriteLine("Is not ok");
                }
        }
}

您可以在此处看到它的运行
http://ideone.com/RDp0X

I fixed the errors I had in my last example here is a test working example

using System;
using System.Text.RegularExpressions;
public class Test
{
        public static void Main()
        {
        Regex rxSingleCharNewLine = new Regex(@"\r(?!\n)|(?<!\r)\n",RegexOptions.Singleline);
        Regex rxNewLine = new Regex(@"\r\n",RegexOptions.Singleline);
                string[] Test = { "Hello,\r\n\r\nWelcome on board of our brand-new cruise line.\r\nKind regards", 
                                         "Hello\r\r\nWelcom on board of our brand-new cruise line.\nKind regards" };

                foreach(string t in Test)
                {
                        System.Console.Write("\"{0}\" ", t.Replace("\r", "\\r").Replace("\n", "\\n"));
                        if(!rxSingleCharNewLine.IsMatch(t) && rxNewLine.IsMatch(t))
                                System.Console.WriteLine("Is ok");
                        else
                                System.Console.WriteLine("Is not ok");
                }
        }
}

You can see it run here
http://ideone.com/RDp0X

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