正则表达式 .net 版本

3
不要问这个是如何工作的,但目前它确实可以 ("^\|*(.*?)\|*$") ... 有点。这将删除所有额外的管道,第一部分,我已经搜索了整个网页,但没有找到答案。我正在使用VB2011 beta、asp web表单和vb编码!
我想捕获特殊字符管道 (|),它用于分隔单词,例如 car|truck|van|cycle
问题在于用户经常在管道之前、之后使用多个空格,并且在管道之前使用空格,例如 |||car||truck | van || cycle
另一个例子:george bush|micheal jordon|bill gates|steve jobs <-- 这样是正确的,但当我去掉空格时,它会把正确的空格也去掉。
因此,我想要消除任何前导、尾随的空格、任何管道前面的空格和管道后面的空格,并且只允许一个管道 (|) 在字母数字字符之间,当然。

你能发一下你的代码吗?如果我们能看到源代码,就更容易理解你想做什么了。 - Katie Kilian
为什么需要使用正则表达式?不能使用Split方法吗?例如:Dim charSeparators() As Char = {"|"c},然后使用yourString.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)。 - fenix2222
2个回答

1

要求:

  • 删除任何前导或尾随的管道符号
  • 在内部术语周围“修整”空格
  • 一次性删除“多个管道符号”

这些是一些样本输入->输出:

"|||car | boat|||" -> "car|boat"
"george bush|micheal jordon|bill gates|steve jobs"
        -> "george bush|micheal jordon|bill gates|steve jobs"
"     george bush|micheal jordon  |bill gates |steve jobs      " 
        -> "george bush|micheal jordon|bill gates|steve jobs"
"123|||123" -> "123|123"

而你的例子,几乎对你有用:

("^\|*(.*?)\|*$")

在我们继续之前,提到这个MSDN参考页面是一个好主意:http://msdn.microsoft.com/en-us/library/az24scfc.aspx

还有这个在线测试页面:http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

我的正则表达式技能不够强,因为我认为这个正则表达式可能有效,但它看起来很困难。我内联记录了文档,但它仍然很复杂(而且完全不起作用)。

^(?:\|*)((?:\s*)([a-zA-Z0-9]?[a-zA-Z0-9 ]*[a-zA-Z0-9]?)(?:\s*)\|?(?:\|*))(?:\|*)$

^                                     - start the line/input
(?:\|*)                               - capture any pipes at the beginning but ignore them
(                                     - begin matching so we can get the values out the other side
(?:\s*)                               - trim leading spaces
[a-zA-Z0-9]?[a-zA-Z0-9 ]*[a-zA-Z0-9]? - match any alphanumerics with spaces in between
(?:\s*)                               - trim trailing spaces
\|                                    - match any one pipe
(?:\|*)                               - ignore any remaining pipes in a row
)*                                    - end matching, we should be done
(?:\|*)                               - capture any pipes at the end but ignore them
$                                     - end of the line/input

那么,让我们来解决这个问题,好吗?

你应该在管道上进行分割,向前查看并查看下一个是否为空长度字符串,如果不是,则将其添加到现有单词长度中。让我们试试:

(我将使用DotNetPad进行此部分)http://dotnetpad.net/ViewPaste/4bpRXD-vZEOwqTLDQbEECg

这是一个样例应用程序,可以以最少的麻烦完成您所需的操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class DotNetPad {
    public static void Main(string[] args) {
        string[] tests = new[] {
            "|||car | boat|||",
            "george bush|micheal jordon|bill gates|steve jobs",
            "     george bush|micheal jordon  |bill gates |steve jobs      ",
            "123|||123"
        };

        foreach(var s in tests)
        Console.WriteLine(CleanString(s));
    }
    public static string CleanString(string input) {
        string result = string.Empty;

        string[] split = input.Split(new[] {
            '|'
        });

        foreach(var s in split) {
            if (!string.IsNullOrEmpty(s)) {
                result += "|" + s.Trim();
            }
        }
        return result.Substring(1);
    }
}

我在第二段代码上最多花了10分钟,自从我编辑帖子尝试让正则表达式起作用以来,一切都变得容易了。故事的寓意是:只做必要的工作,不必为所有事情都使用正则表达式。

1
我会从删除空格开始:

MyString = Regex.Replace(MyString, "[ ]*\|[ ]*", "|")

然后是多个管道:

MyString = Regex.Replace(MyString, "\|{2,}", "|")

例如。
Dim MyString As String = "car  | truck ers  ||van|||cycle"

提供

 "car|truck ers|van|cycle"

是的,如果他仍然想使用正则表达式,我建议使用多个重复的正则表达式。但是,我认为数组分割和连接更快,只因为它更简单易懂,因为如果他要使用正则表达式,他可以直接在遇到的字符串上本地使用string.replace(一次用于管道,两次用于管道周围的空格)。 - jcolebrand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接