用正则表达式拆分字符串并保留分隔符 [英] Splitting a string with Regex and keep the separator

查看:556
本文介绍了用正则表达式拆分字符串并保留分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当存在一个分隔符字符串时,我需要分割一个字符串(让我们看看
sey #Key(val),其中val是一个变量)并将其重新加入到正确的
$ b $中b进行一些处理后的订单。


有没有办法使用Regex.Split函数在#Key(val)出现时拆分字符串

但是这样可以保持#Key(val)

的出现,我可以在对每个令牌执行某些操作后重构最终字符串(我需要基本上转换每个令牌) />
将字符串转换为字符数组但我需要以不同的方式执行此操作

是字符串是#Key(val)


谢谢。

Andrea

I need to split a string whenever a separator string is present (lets
sey #Key(val) where val is a variable) and rejoin it in the proper
order after doing some processing.

Is there a way to use the Regex.Split function to split the string
whenever the #Key(val) occurrs but that keeps the #Key(val)
occurrences to that I can reconstruct the final string after doing
certain operations on each token (I need to basically convert each
string into an array of characters but I need to do this differently
is the string is a #Key(val)

Thanks.
Andrea

推荐答案

* na ***@community.nosp 上午写道,2007年4月6日23:16:
* na***@community.nospam wrote, On 4-6-2007 23:16:

我需要在分隔符字符串时拆分字符串存在(允许

sey #Key(val),其中val是变量)和rejo在做了一些处理之后用适当的

命令。


有没有办法使用Regex.Split函数来分割字符串
每当#Key(val)出现时
但是保持#Key(val)

出现的次数我可以在执行某些操作后重建最终字符串

在每个令牌上(我需要基本上将每个

字符串转换为字符数组但我需要以不同方式执行此操作

是字符串是#Key(val)


谢谢。

Andrea
I need to split a string whenever a separator string is present (lets
sey #Key(val) where val is a variable) and rejoin it in the proper
order after doing some processing.

Is there a way to use the Regex.Split function to split the string
whenever the #Key(val) occurrs but that keeps the #Key(val)
occurrences to that I can reconstruct the final string after doing
certain operations on each token (I need to basically convert each
string into an array of characters but I need to do this differently
is the string is a #Key(val)

Thanks.
Andrea



此代码以不同方式分割每一行:


正则表达式rx =新正则表达式(@"(?=#\ w + \(\ w + \))|(?< =#\ w + \(\ w + \\ \\))",

RegexOptions.None);

string [] arr = rx.Split(输入);


它寻找模式开头或结尾的每一点

你正在寻找。我认为它不是大输入的最快,但是我没有测试过



你可能会更好地尝试使用MatchEvaluator还有一个很好的

写的替换电话,但是为了帮助你,我需要更多的信息来确定你将要做什么样的字符串操作。


Jesse

This code splits each line differently:

Regex rx = new Regex(@"(?=#\w+\(\w+\))|(?<=#\w+\(\w+\))",
RegexOptions.None);
string[] arr = rx.Split(input);

It looks for every point at the beginning or the end of the pattern
you''re looking for. it isn''t the fastest on large inputs I guess, but I
haven''t tested.

You might be better off experimenting with a MatchEvaluator and a well
written replace call, but to help you with that I''d need a little more
info on what kind of string manipulation you''d be doing.

Jesse


* na ** *@community.nosp 上午写道,在4-6-2007 23:16:
* na***@community.nospam wrote, On 4-6-2007 23:16:

每当有一个分隔符字符串时我需要拆分一个字符串(允许

sey #Key(val),其中val是一个变量)并在进行一些处理后以适当的

顺序重新加入。


有没有办法使用Regex.Split函数在#Key(val)出现时拆分字符串

但保持#Key(val)

出现之后,我可以在e上执行某些操作后重建最终字符串ach令牌(我需要基本上将每个

字符串转换为字符数组但我需要以不同方式执行此操作

字符串是#Key(val)


谢谢。

Andrea
I need to split a string whenever a separator string is present (lets
sey #Key(val) where val is a variable) and rejoin it in the proper
order after doing some processing.

Is there a way to use the Regex.Split function to split the string
whenever the #Key(val) occurrs but that keeps the #Key(val)
occurrences to that I can reconstruct the final string after doing
certain operations on each token (I need to basically convert each
string into an array of characters but I need to do this differently
is the string is a #Key(val)

Thanks.
Andrea



这应该更好:

Regex rx2 = new

正则表达式(@"(?< keyval>#\ w + \(\ w + \))|(?< other>( (?!#\ w + \(\ w + \))。)*)",

RegexOptions.None);

string result = rx2。替换(输入,新的

MatchEvaluator(ManipulateString));


私有字符串ManipulateString(匹配目标)

{

if(target.Groups [" keyval"]。成功)

{

返回ManupulateKeyVal(target.Groups [" keyval" ] .Value);

}


else if(target.Groups [" other"]。Success)

{

返回ManupulateOther(target.Groups [" other"]。Value);

}

}

这将传递找到的碎片以便操作函数和

完成后将结果传递给新的字符串。


亲切的问候,


Jesse

This should work even better:

Regex rx2 = new
Regex(@"(?<keyval>#\w+\(\w+\))|(?<other>((?!#\w+\( \w+\)).)*)",
RegexOptions.None);
string result = rx2.Replace("input", new
MatchEvaluator(ManipulateString));

private string ManipulateString(Match target)
{
if (target.Groups["keyval"].Success)
{
return ManupulateKeyVal(target.Groups["keyval"].Value);
}

else if (target.Groups["other"].Success)
{
return ManupulateOther(target.Groups["other"].Value);
}
}

This will pass the found pieces in order to the manipulate function and
pass the result into a new string when done.

Kind regards,

Jesse


嗨Andrea,

我不确定我是否完全理解你的问题。如果Jesse的回复有帮助,请您告诉我们

吗?谢谢。

问候,

Walter Wang(wa****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


================================= =================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子提供按现状 ;没有保证,也没有授予任何权利。

Hi Andrea,

I''m not sure if I fully understand your question. Would you please let us
know if Jesse''s reply helps? Thanks.
Regards,
Walter Wang (wa****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于用正则表达式拆分字符串并保留分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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