正则表达式:初始文本后的多个匹配项 [英] regex: multiple matches after initial text
问题描述
我正试图在一些初始文本之后匹配一组匹配:
mytext:some" " somethingelse" 另一件事
可能是另一件事
(?:mytext :)(?< mymatch> ["] {1,1} [^ ;] + ["] {1,1} [\ s |] +)+
我只得到最后一个可能是另一个。我希望将所有值与
引号组合在一起,因此在()之后的+。
这可能吗?
Chance Hopkins写道:我试图在一些初始文本后匹配一组匹配:
mytext:某事 " somethingelse" 另一件事
也许是另一件事
(?:mytext :)(?< mymatch> ["] {1,1} [^] + ["] { 1,1} [\ s |] +)+
我只得到最后一个也许是另一个。我想把所有的值都用
引号作为一个组,因此在()之后是+。
这可能吗?
正则表达式正则表达式=新正则表达式(@"
((?< mymatch>(?< ="")\ w + \\\ * \ w + (?="")| [^ \s""] +))",
RegexOptions.ExplicitCapture);
测试输入:
某事 " somethingelse" 另一件事 可能是另一个
测试输出:
mymatch =?某事?=
mymatch =?somethingelse?=
mymatch =?另一件事?=
mymatch =?也许是另一回事?=
-
小心点,
肯
(直接回复,移除酷车。< sigh>)
" Ken Arway" < KA **** @ jaguar.att.net>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...Chance Hopkins写道:我试图在一些初始文本后匹配一组匹配:
mytext:some" " somethingelse" 另一件事
也许是另一件事
(?:mytext :)(?< mymatch> ["] {1,1} [^] + ["] { 1,1} [\ s |] +)+
我只得到最后一个也许是另一个。我希望得到所有值的引号作为一个组,因此在()之后的+。
这可能吗?
RegexOptions.ExplicitCapture);
测试输入:
某事物 " somethingelse" 另一件事 也许是另一个
测试输出:
mymatch =?某事?=
mymatch =?somethingelse?=
mymatch =?另一件事?=
mymatch =?也许是另一个?=
-
保重,
Ken
(直接回复,移走酷车。< sigh>)
感谢您的帮助。
是否可以在初始匹配后定位此匹配(因此我只是
在文本mytext:之后匹配"''s,而不首先拆分字符串?
我敢肯定我能使用带有IndexOfmytext:的SubString,但是如果可能的话,我更愿意用RegEx做这个
。
我是使用300mhz处理器在PPC上工作,并且必须在案例陈述中做大量的这些内容。它有一个运行
循环的线程,我真的需要尝试尽可能小。
再次感谢。
Hello Chance,
mytext:" something " somethingelse" 另一件事
也许是另一件事
也许你想要一个更通用的方式:
正则表达式正则表达式=新的正则表达式(@"(?< ="")(\ s * \w + \ * *)+(?="")");
foreach(在regex.Matches中匹配匹配(inputText.Text))
outputText.AppendText(match.Value +" \\\\ n");
所以你也得到了另一个第三个令牌字样。等等。
ciao Frank
-
Dipl.Inf。 Frank Dzaebel [MCP C#]
http://Dzaebel.NET
I''m trying to match a set of matches after some initial text:
mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+
I only get the last one "maybe another". I want to get all the values with
quotes as a group, hence the + after the ()''s.
Is this possible?
Chance Hopkins wrote:I''m trying to match a set of matches after some initial text:
mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+
I only get the last one "maybe another". I want to get all the values with
quotes as a group, hence the + after the ()''s.
Is this possible?
Regex regex = new Regex(@"
((?<mymatch>(?<="")\w+\s*\w+(?="")|[^\s""]+))",
RegexOptions.ExplicitCapture);
Test input:
"something" "somethingelse" "another thing" "maybe another"
Test output:
mymatch =?something?=
mymatch =?somethingelse?=
mymatch =?another thing?=
mymatch =?maybe another?=
--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
"Ken Arway" <ka****@jaguar.att.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...Chance Hopkins wrote:I''m trying to match a set of matches after some initial text:
mytext: "something" "somethingelse" "another thing"
"maybe another"
(?:mytext: )(?<mymatch>["]{1,1}[^"]+["]{1,1}[\s| ]+)+
I only get the last one "maybe another". I want to get all the values
with quotes as a group, hence the + after the ()''s.
Is this possible?
Regex regex = new Regex(@" ((?<mymatch>(?<="")\w+\s*\w+(?="")|[^\s""]+))",
RegexOptions.ExplicitCapture);
Test input:
"something" "somethingelse" "another thing" "maybe another"
Test output:
mymatch =?something?=
mymatch =?somethingelse?=
mymatch =?another thing?=
mymatch =?maybe another?=
--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Thanks for the help.
Is it possible to target this match after an initial match (so that I only
match ""''s after the text "mytext: "), without splitting the string first?
I''m sure I could use SubString with IndexOf "mytext: ", but I''d prefer to do
this all with RegEx if possible.
I''m working on a PPC with a 300mhz processor and have to do a large number
of these inside a case statement to begin with. It''s got a thread running a
loop and I really need to try and be as minimal as possible.
Thanks again.
Hello Chance,
mytext: "something" "somethingelse" "another thing"
"maybe another"
maybe you want it a more generic way :
Regex regex = new Regex(@"(?<="")(\s*\w+\s*)+(?="")");
foreach (Match match in regex.Matches(inputText.Text))
outputText.AppendText(match.Value+"\r\n");
so you get also the words "another third token" etc.
ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP C#]
http://Dzaebel.NET
这篇关于正则表达式:初始文本后的多个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!