用于删除句点的正则表达式代码

时间:2015-03-27 12:00:37

标签: .net regex vb.net

Dim wr_str as string

Dim str as string = "Introduction........................1"

Dim no As Match = Regex.Match(wr_str, "(^.*?)(\s*)([0-9]+)\s*$")

wr_str = Regex.Replace(wr_str, "(^.*?)(\s*)([0-9]+)\s*$", no.groups(1).value & no.groups(3).value)

Input string = "Introduction........................1"

我要求输出字符串为'简介; 1'。

请您告诉我如何更改正则表达式以忽略文本

之后的点

4 个答案:

答案 0 :(得分:1)

您可以使用这样的捕获组:

Dim myMatch = Regex.Match("Introduction.............1", "(^.*?)\.*([0-9]+)\s*$")
Dim res2 = myMatch.Groups(1).Value + ";" + myMatch.Groups(2).Value

使用\.*捕获点,并且不会将其用于最终字符串创建。

编辑:匹配您的代码上下文:

Dim no as Match = Regex.Match("Introduction.............1", "(^.*?)\.*([0-9]+)\s*$")
wr_str = Regex.Replace(wr_str, "(^.*?)(\.*)([0-9]+)\s*$", no.Groups(1).Value.Replace(".", "").Trim() & ";" & no.groups(2).value)

答案 1 :(得分:0)

以最简单的形式,这应该有效:

Dim wr_str as string

Dim str as string = "Introduction........................1"

Dim no As Match = Regex.Match(wr_str, "(^.*?)(\.*)([0-9]+)\s*$")

wr_str = Regex.Replace(wr_str, "(^.*?)(\.*)([0-9]+)\s*$", no.groups(1).value & ";" & no.groups(3).value)

正则表达式在空格上匹配,而不是点。

答案 2 :(得分:0)

似乎这样做:

wr_str = Regex.Replace(wr_str, "\\.+", ";")

答案 3 :(得分:0)

试试这个正则表达式模式(ignorecase + multiline):

^(.*?)\.*?(\d+)$

了解详情 - regex tester

http://i.imgur.com/qrTMsHr.png