regex.split,我可以保留分隔符 [英] regex.split, can I keep the delimeter

查看:339
本文介绍了regex.split,我可以保留分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于文本分隔符拆分大文件,但是我不想要从字符串中留下的分隔符
。例如,如果我有一个字符串

" NAME:Bill TOWN:Helena NAME:Frank TOWN:Helena"

我想分开NAME:给我


s(0)=姓名:Bill TOWN:Helena

s(1)=姓名:Frank TOWN:Helena


***通过开发人员指南 http://www.developersdex.com 发送***

不要只是参加USENET ......获得奖励!

解决方案

Claud,


为什么你不只是使用标准字符串拆分而不是在这个

的情况下只耗费时间的正则表达式。


然后在之前添加该名称是一块蛋糕,你可以在你没有使用正则表达式获胜的时候轻松做到这一点。

$ b每个str的$ b为myarray中的String

str =" NAME:" &安培; str $ / $
next


我希望这会有所帮助,


Cor


我使用了regex.split,因为它将整个字符串识别为

分隔符。 String.split分割在分隔符中的每个字符上,如

这个:

N

A

M

E

:Bill TOW

N

:H

el

e

n

a


我可能会和一个字符串构建者一起追加数据,但我是

希望有办法解决这个问题。


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!


尝试一下:


Dim str As String =" NAME:Bill TOWN:Helena NAME:Frank TOWN: Helena"

Dim reg As New Regex("(\\\?NAME:\ [[az] + \ t TOWN:\s [az] + \s?)" ;,

RegexOptions.IgnoreCase)

Dim mtch匹配

每个mtch in reg.Matches(str)

Debug.WriteLine(mtch .Value)

下一页


" Claud Balls" <李** @ trainer.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...

我用过regex.split因为它将整个字符串识别为
分隔符。 String.split分裂在分隔符中的每个字符上,如
这个:
A
M
E
:Bill TOW
N
:H


















是希望有一种解决方法。

***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET ......获得奖励!



I am splitting large files based on a text delimeter, but I don''t want
the delimeter left out of the string. For example if I had a string
"NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena"
I would want to split on NAME: giving me

s(0) = NAME: Bill TOWN: Helena
s(1) = NAME: Frank TOWN: Helena

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

解决方案

Claud,

Why don''t you just use the standard string split in stead of the in this
case only time consuming Regex.

And adding that name before afterwards is a piece of cake, that you can do
probably easy in the time you win by not using the Regex for this.

for each str as String in myarray
str = "NAME:" & str
next

I hope this helps,

Cor


I used regex.split because it recognized the whole string as the
delimeter. String.split split on every character in the delimeter like
this:
N
A
M
E
: Bill TOW
N
: H
el
e
n
a

And I''ll probably go with a stringbuilder to append the data, but I was
hoping there was a way around that.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


Give this a try:

Dim str As String = "NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena"
Dim reg As New Regex("(\s?NAME:\s [a-z]+\s TOWN:\s [a-z]+\s?)",
RegexOptions.IgnoreCase)
Dim mtch As Match
For Each mtch In reg.Matches(str)
Debug.WriteLine(mtch.Value)
Next

"Claud Balls" <Li**@trainer.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

I used regex.split because it recognized the whole string as the
delimeter. String.split split on every character in the delimeter like
this:
N
A
M
E
: Bill TOW
N
: H
el
e
n
a

And I''ll probably go with a stringbuilder to append the data, but I was
hoping there was a way around that.

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



这篇关于regex.split,我可以保留分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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