Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何拆分长文本字符串并为每个字段添加换行符_Asp.net_Vb.net - Fatal编程技术网

Asp.net 如何拆分长文本字符串并为每个字段添加换行符

Asp.net 如何拆分长文本字符串并为每个字段添加换行符,asp.net,vb.net,Asp.net,Vb.net,我有一个长字段,其中包含如下字符串: 1-脚本2-白色3-EMB-14-*5-*6-彼得森夫人7-爱8-彼得森先生9-*10-*11-*12-9990007878 13-客户输入:Flash应用程序14-15-71 16-849b5629d0144e3c8293200910742e0d 我需要找到一种方法来操作字符串,并将其格式化为: 1-脚本 2-白色 3-EMB-1 4-* 5-* 6-彼得森夫人 7-爱 8-彼得森先生 9-* 10-* 11-* 12-9990007878 13-由客户输

我有一个长字段,其中包含如下字符串:

1-脚本2-白色3-EMB-14-*5-*6-彼得森夫人7-爱8-彼得森先生9-*10-*11-*12-9990007878 13-客户输入:Flash应用程序14-15-71 16-849b5629d0144e3c8293200910742e0d

我需要找到一种方法来操作字符串,并将其格式化为:

1-脚本
2-白色
3-EMB-1
4-*
5-*
6-彼得森夫人
7-爱
8-彼得森先生
9-*
10-*
11-*
12-9990007878
13-由客户输入:Flash应用程序
14-
15-71
16-849b5629d0144e3c8293200910742e0d


表示新字段开头的字符是数字+'-'序列。

这里有两个选项,取决于您是否需要保留前缀数字,您有两个选项。对于两者,我都使用正则表达式来帮助检测每个字段的边界

下面是基本正则表达式:

\d+-\s
正则表达式查找一个或多个数字(0-9)、连字符和空格。这与您提供的模式相匹配

0- Test   -- MATCH
1 - Test  -- No Match, space between the integer and hyphen.
9999-ABC  -- No Match, there is not a space between the hyphen and the value.
选项1

这将从字符串中删除前缀

Dim str As String = "1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14- 15- 71 16- 849b5629d0144e3c8293200910742e0d"

Dim items = Regex.Split(str, "\d+-\s", RegexOptions.None, New TimeSpan(0, 0, 30))
选项2 这将保留前缀,并将值放入组中以便于访问

通过使用反向look behinds,此方法中的正则表达式稍微复杂一些。这样我们就可以确定我们捕获的值是否是字段值的一部分

编辑我更改了此选项,以使用StringBuilder重建带分隔符的字符串。这是下面的评论

Dim str As String = "1- SCRIPT 2- WHITE 3- EMB-1 4- * 5- * 6- Mrs. Petersen 7- loves 8- Mr. Petersen 9- * 10- * 11- * 12- 9990007878 13- Entered by customer: Flash App 14- 15- 71 16- 849b5629d0144e3c8293200910742e0d"

Dim matches = Regex.Matches(str, "(?<fieldNumber>\d+)-\s(?<fieldValue>(?:(?!\d+-\s).)*)",   RegexOptions.None, New TimeSpan(0, 0, 30))
Dim sb As New StringBuilder
For Each match As Match In matches
    sb.AppendFormat("{0}\n", match.Value)
    Dim matchedString = match.Value
    Dim fieldNumber = match.Groups("fieldNumber").Value
    Dim fieldValue = match.Groups("fieldValue").Value
Next

Dim entireString = sb.ToString()
响应* (为了清楚起见,本节来自OP)

我是这样添加代码的:

DirectCast(e.Item.FindControl("litNote"), Literal).Text = Regex.Replace(product.Note, "(\d+-\s(?:(?!\d+-\s).)*)", "$1\n", RegexOptions.IgnorePatternWhitespace)
当我运行页面时,我看到的是(使用稍微不同的字符串):

1-标志1-测试线1\n2-标志1-测试线2\n3-标志1-测试线3\n4-标志1-测试线4\n5-标志1-测试线5\n6-电话:1111111111\n7-由客户输入\n8-注释:这是对标志1\n9-5线标志的测试,样式:98\n10-SKU:10140,数量:1\n 11-EE69EF2AF024A458488DE10F498AC10\n

编辑

我想我需要替换:“$1\n”

带“$1

编辑以响应

您是正确的,根据返回输出的位置,您可能需要一个

而不是
\n
。我不确定您是否使用了

(\d+               -- look for one or more digits (0-9)
-                  -- look for a hyphen
\s                 -- look for a space
(?:                -- start a non-capturing group
  (?!\d+-\s).      -- do a negative look behind to see if we're inside of the delimited portion 
)*                 -- capture zero or more characters that satisfy the negative look behind (this and the negative look behind is the magic. It's what stops the match so that the next field can start)  
)
如果您不熟悉正则表达式,那么我建议您阅读基础知识,以便所有这些都有意义

一个好的参考网站

下面是关于后面的部分,这可能是正则表达式中令人困惑的部分:


嗨,内森

到目前为止,我就是这样实施您的选项2的

 Dim str As String
 Dim sb As New StringBuilder
 Dim fieldNumber As String
 Dim fieldValue As String            
 str = product.Note
 Dim matches = Regex.Matches(str, "(?<fieldNumber>\d+)-\s(?<fieldValue>(?:(?!\d+-\s).)*)", RegexOptions.None)
 For Each match As Match In matches
    fieldNumber = match.Groups("fieldNumber").Value
    fieldValue = match.Groups("fieldValue").Value
    sb.AppendFormat("<p><label>{0}</label><span>{1}</span></p>", fieldNumber, fieldValue)
 Next
 DirectCast(e.Item.FindControl("litNote"), Literal).Text = sb.ToString()
Dim str作为字符串
使某人成为新的架线工
将字段编号设置为字符串
将字段值设置为字符串
str=产品。注
Dim matches=Regex.matches(str,(?\d+)-\s(?((?!\d+-\s)。*)”,RegexOptions.None)
将每个比赛作为比赛中的比赛
fieldNumber=match.Groups(“fieldNumber”).值
fieldValue=match.Groups(“fieldValue”).Value
sb.AppendFormat(“{0}
”,字段值) 下一个 DirectCast(例如Item.FindControl(“litNote”),Literal.Text=sb.ToString()
我很难理解如何操纵“fieldNumber”中的值


下面是我认为你正在尝试做的事情。您要做的样式只是将fieldNumber转换为int,然后您可以使用它来确定您所在的字段

Dim str作为字符串
使某人成为新的架线工
将字段编号设置为字符串
将字段值设置为字符串
str=产品。注
Dim matches=Regex.matches(str,(?\d+)-\s(?((?!\d+-\s)。*)”,RegexOptions.None)
将每个比赛作为比赛中的比赛
fieldNumber=match.Groups(“fieldNumber”).值
fieldValue=match.Groups(“fieldValue”).Value
sb.AppendFormat(“{0}{1}

”,字段号,字段值) 下一个 DirectCast(例如Item.FindControl(“litNote”),Literal.Text=sb.ToString()
输入有任何限制吗?例如,连字符(-)可以显示在输入中,除了记录分隔符中的数字之外?数字总是1到16还是会有所不同?是的,可能会有连字号,如:1-签名1-测试行12-签名1-测试行23-签名1-测试行34-签名1-测试行45-签名1-测试行56-电话:1111111,但记录分隔符总是“数字-空格”到目前为止,它上升到16,但这可能会在未来发生变化多谢,请不要只要求我们为您解决问题。向我们展示你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“”,以获取您真正需要阅读的优秀文章。当我尝试使用选项1时,我在以下方面遇到错误:Dim items=Regex.Split(Str,“\d+-\s”,RegexOptions.None,New TimeSpan(0,0,30))-重载解析失败,因为没有可访问的“Split”接受此数量的参数。我不知道是否需要使用数组,因为现在文本字段(prod.Note)被分配给aspx页面上的多行字段,如下所示:DirectCast(例如Item.FindControl(“litNote”),Literal.text=prod.Note WI需要能够首先通过模式识别每个字段,然后添加换行符,回车,这样每个字段都作为一个块放在html页面上。解决失败的问题是因为我使用的是.Net 4.5,我假设您使用的是旧版本。如果删除新的Timespan(0,0,30)参数,它将对您有效。如果我只想将结果分配给页面上的一个多字段,这是否有效?我添加了您的代码,其中有一点变化Dim matches=Regex.matches(product.Note,“(?\d+)-\s(?((?!\d+-\s)。*”,RegexOptions.None)将每个匹配作为中的匹配
        Dim str As String
        Dim sb As New StringBuilder
        Dim fieldNumber As String
        Dim fieldValue As String            
        str = product.Note
        Dim matches = Regex.Matches(str, "(?<fieldNumber>\d+)-\s(?<fieldValue>(?:(?!\d+-\s).)*)", RegexOptions.None)
        For Each match As Match In matches
            fieldNumber = match.Groups("fieldNumber").Value
            fieldValue = match.Groups("fieldValue").Value
            sb.AppendFormat("{0}<br />", fieldValue)
        Next
        DirectCast(e.Item.FindControl("litNote"), Literal).Text = sb.ToString()
 Dim str As String
 Dim sb As New StringBuilder
 Dim fieldNumber As String
 Dim fieldValue As String            
 str = product.Note
 Dim matches = Regex.Matches(str, "(?<fieldNumber>\d+)-\s(?<fieldValue>(?:(?!\d+-\s).)*)", RegexOptions.None)
 For Each match As Match In matches
    fieldNumber = match.Groups("fieldNumber").Value
    fieldValue = match.Groups("fieldValue").Value
    sb.AppendFormat("<p><label>{0}</label><span>{1}</span></p>", fieldNumber, fieldValue)
 Next
 DirectCast(e.Item.FindControl("litNote"), Literal).Text = sb.ToString()