逗号之间的Vb.net正则表达式函数值(“,”,“) [英] Vb.net regex function values between commas (",")

查看:137
本文介绍了逗号之间的Vb.net正则表达式函数值(“,”,“)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在两个逗号(,)之间进行所有匹配。到目前为止,我有一个代码,但它并没有像我期望的那样工作。要搜索的文本是RichTextBox.Text属性。



例如我写的,5,9,12,17,4,8,11和我的正则表达式代码返回,5,|,12,|,4,|,11,

我知道代码在做什么,它正在搜索AB,CD,EF,但我需要类似AB的东西, BC,CD等。如何修改它以返回所有值



  .Text =   
Dim 正则表达式作为 正则表达式( \,(。*?)\,

对于 每个 m 作为匹配正则表达式.Matches(RichTextBox1.Text)
Me .Text + = m.Value& |
下一页





我的尝试:



--------------------------------------------- -------

解决方案

您的模式当前使用前导和尾随逗号,因此会跳过其他每个值。



您需要使用零宽度正向前瞻/后观断言:

正则表达式语言 - 快速参考 [ ^ ]



此外,最好使用 [^ \,] * 而不是。*?,因为你知道除了逗号之外你想要匹配任何东西。



最后,如果你正在做stri在循环中连接,你应该使用 StringBuilder

  Dim 正则表达式作为 正则表达式( (?< = \,)([^ \,] *)(?= \,)
Dim sb 作为 系统。 Text.StringBuilder()

对于 每个 m 作为匹配 regex.Matches(RichTextBox1.Text)
sb.Append(m.Value)
sb .Append( |
Next

.Text = sb.ToString()


这不是我的,但我忘记了我得到了它(它位于拆分CSV数据下的有用的正则表达式集合中)

(?:^ |,)(?= [^ ] |()) (* | [^ ] *) - ((1)[^?)?(=,|?



我?我在逗号上使用String.Split,并丢弃空值而不是使用正则表达式。


I try to get all matches between two commas (","). I have so far a code but it isnt working as i expect it. The text to be searched in is the RichTextBox.Text Property.

For example i write ,5,9,12,17,4,8,11, and my regex code returns ,5,|,12,|,4,|,11,
I know what the code is doing, it is searching A-B, C-D, E-F, but i need something like A-B, B-C, C-D etc. How to modify it to return all values

Me.Text = ""
Dim regex As New Regex("\,(.*?)\,")

For Each m As Match In regex.Matches(RichTextBox1.Text)
    Me.Text += m.Value & "|"
Next



What I have tried:

----------------------------------------------------

解决方案

Your pattern currently consumes the leading and trailing commas, so every other value is skipped.

You need to use zero-width positive lookahead/lookbehind assertions instead:
Regular Expression Language - Quick Reference[^]

Also, it would be better to use [^\,]* rather than .*?, since you know you want to match anything except a comma.

And finally, if you're doing string concatenation in a loop, you should use a StringBuilder instead.

Dim regex As New Regex("(?<=\,)([^\,]*)(?=\,)")
Dim sb As New System.Text.StringBuilder()

For Each m As Match in regex.Matches(RichTextBox1.Text)
    sb.Append(m.Value)
    sb.Append("|")
Next

Me.Text = sb.ToString()


This isn't mine, but I forget where I got it (it was sitting in my "useful regexes" collection under "split CSV data")

(?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|


)


Me? I'd use String.Split on the comma, and discard empty values rather than use a regex.


这篇关于逗号之间的Vb.net正则表达式函数值(“,”,“)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆