[Vb.net]如何使用多个分隔符拆分字符串但保持分隔符 [英] [Vb.net] how to split string with multiple delimeter but keep delimeter
问题描述
我有一个字符串:
string ="Yellow.car Red.Bag Blue.eyes"
有没有一种方法可以在句点和空白处拆分字符串,但只能将句点保留在数组中?
[黄色",'',"汽车,"红色,"'',袋",蓝色",''," 眼睛"]
最好使用string.split(regexp)的正则表达式
我尝试过的事情:
Dim arr As String()= Regex.Split(TextBox1.Text,(.|,)")
对于每个i作为arr中的字符串
Console.WriteLine(i)
Next
I have a string:
string = "Yellow.car Red.Bag Blue.eyes"
Is there a way to split the string on both periods and whitespaces, but only retain the periods inside the array?
[''Yellow'',''.'',''Car'',''Red'',''.'',''Bag'',''Blue'',''.'',''Eyes'']
A regex for string.split(regexp) would be preferable
What I have tried:
Dim arr As String() = Regex.Split(TextBox1.Text, "(.|,)")
For Each i As String In arr
Console.WriteLine(i)
Next
推荐答案
用另一个包含句点的令牌替换句点;然后拆分为新令牌.
例如如果要分割."并保留,然后替换为."和 "@."或者 ".@";
Replace the periods with another token that includes a period; then split on the new token.
e.g. if you want to split on "." and keep, then replace "." with "@." or ".@"; then split on "@".
在正则表达式中,点.''具有特定含义:任何字符".因此,"a.b"的正则表达式将匹配"aab","abb","acb",... a和b之间的任何字符.
因此,您的正则表达式:(.|,)"是任何字符或逗号"-等于任何字符!
您可以对其进行转义,使其变为文字:(\.|,)",它是点或逗号",但是对于单个字符值,则有一个更好的语法:"[\.,]"可以执行相同的操作
In a Regex, dot ''.'' has a specific meaning: "any character". So a regex of "a.b" will match "aab", "abb", "acb", ... any character between an a and a b.
So your regex: "(.|,)" is "either any character or a comma" - which amounts to "any character!
You can escape it so that it becomes a literal: "(\.|,)" which is "either dot or comma" but for single character values there is a better syntax: "[\.,]" which does the same thing.
最好使用string.split(regexp)的正则表达式
A regex for string.split(regexp) would be preferable
建议:学习RegEx,有多种使用方法.
Split
用于在分隔符上剪切字符串,并且分隔符丢失.
另一端的Match
将列出字符串中每个匹配项的列表.您只需要构建一个匹配一个单词或一个点"的RegEx.
像\w+|\.
这样的RegEx应该满足您的所有需求.
只是一些有趣的链接,可帮助您构建和调试RegEx.
这是RegEx文档的链接:
perlre-perldoc.perl.org [ .NET Regex测试器-Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:学习,构建和开发&测试RegEx [^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这段代码向您展示了RegEx,它是一个很好的图形,它对理解RegEx的工作很有帮助: Debuggex:在线可视化regex测试器. JavaScript,Python和PCRE. [ ^ ]
该网站还以漂亮的图形显示了Regex,但无法测试与RegEx匹配的内容: Regexper [
Advice: study RegEx, there is more than 1 way to use it.
Split
is used to cut a string on separator and separator is lost.
Match
on the other side will make a list of every match in string. you just have to build a RegEx that will match "a word or a point".
A RegEx like \w+|\.
should all what you want.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can''t test what match the RegEx: Regexper[^]
这篇关于[Vb.net]如何使用多个分隔符拆分字符串但保持分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!