简体   繁体   English

正则表达式代码删除句点

[英]regex code to remove periods

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"

I require the output string as 'Introduction;1'. 我要求输出字符串为“ Introduction; 1”。

Could you please let me know how to change the regex expression to ignore the dots after the text 您能否让我知道如何更改正则表达式以忽略文本后的点

You can use capturing groups like this: 您可以像这样使用捕获组:

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

Dots are captured with \\.* , and are not used for final string creation. 点用\\.*捕获,并且不用于最终的字符串创建。

EDIT: To match your code context: 编辑:要匹配您的代码上下文:

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)

In it's simplest form, this should work: 以最简单的形式,它应该可以工作:

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)

The regex was matching on whitespace, not dots. 正则表达式匹配空白,而不是点。

看来这可以做到:

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

Try this regex pattern (ignorecase+multiline): 尝试以下正则表达式模式(ignorecase +多行):

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

See more - regex tester 查看更多-regex测试仪

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM