需要一些模式帮助 [英] need some pattern help

查看:103
本文介绍了需要一些模式帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的字符串:

[[丈夫名字]]先生与[[妻子名字]]夫人结婚,并且有[[num kids]]个孩子

我需要一种模式,该模式可以拉出方括号中和非方括号中的文本,因此最终结果是这样的:

先生.
"[[丈夫名]]"
已嫁给太太."
"[[妻子的名字]]"
",他们有"
"[[num kids]]"
孩子们"

下面的模式将括号中的内容很好地抓住了(括号中的文本始终只能是字母,空格和数字),但我无法提出或"部分以获取不在文本中的文本.方括号(此文本可以由任何字符组成).

\ [\ [[a-zA-Z0-9 \ s] * \] \]

我认为在正确使用模式之后,我应该能够使用该模式将单个分解的字符串读取到可以循环通过的数组中,以便可以通过用一些字段值替换带括号的文本来构建完成的字符串.我不完全确定如何将模式匹配项放入数组中.我尝试了一些似乎无效的方法,但我认为这主要是因为我的模式是错误的.对此的任何帮助也将不胜感激.

谢谢,

禽兽.


此外,被测试的字符串在字符串的开头或结尾可能带有也可能没有括号.它可能看起来像这样:

[[丈夫名字]]嫁给了[[妻子名字]]夫人,他们的孩子人数为[[num kids]]


似乎我应该能够做到这一点:

\ [\ [[a-zA-Z0-9 \ s] * \] \]或不是\ [\ [[a-zA-Z0-9 \ s] * \] \]

但我不知道如何制作正确的正则表达式模式

这类作品:

(\ [\ [[a-zA-Z0-9 \ s] * \] \])|([^(\ [\ [)])*

但如果[[和]之间的空格中有[],则将它们排除在外.

仅提供一些背景知识:最终用户可以从看起来像[[丈夫姓名]]或[[num kids]]等的域代码列表中进行选择.这些是固定项目,可以选择并放入网格单元中.间隙单元(可以这么说)可以是用户选择输入的任何文本.我当然可以防止最终用户在用户定义的单元格中输入[[AND]],但我不想阻止单个"["或]",甚至不能阻止两个"[["或double]]]".由于我绝对将域代码限制为[a-zA-Z0-9 \ s],并且我可以防止最终用户输入[[]],因此域代码和最终用户输入的数据.如果必须阻止输入[[或]],我会这样做,但我不想限制它.

I have some strings like this:

Mr. [[husband name]] is married to Mrs. [[wife name]] and they have [[num kids]] kids

I need a pattern that will pull out the text that''s both in the brackets and the text that''s not in the brackets so my end result is like this:

"Mr. "
"[[husband name]]"
" is married to Mrs. "
"[[wife name]]"
" and they have "
"[[num kids]]"
" kids"

The pattern below grabs the stuff in the brackets just fine (bracketed text will always only be letters, spaces and numbers) but I haven''t been able to come up with the OR part that gets the text that''s not in the brackets (this text can be made up of any characters).

\[\[[a-zA-Z0-9\s]*\]\]

I think after I get the pattern right I should be able to use that pattern to read the individual broken down strings into an array that I can loop through so I can build the finished string by replacing the bracketed text with some field values. I''m not entirely sure how to get the pattern matches into the array. I tried a few things that didn''t seem to work but I think it was mostly because my pattern was wrong. Any help on that would be appreciated as well.

Thanks,

Avian.


Additionally the string being tested may or may not have unbracketed text at the beginning or the end of the string. It could look like this:

[[husband name]] is married to Mrs. [[wife name]] and the number of kids they have is [[num kids]]


Seems like I should just be able to do this:

\[\[[a-zA-Z0-9\s]*\]\] OR NOT \[\[[a-zA-Z0-9\s]*\]\]

but I don''t know how to make that a correct regex pattern

This kind of works:

(\[\[[a-zA-Z0-9\s]*\]\])|([^(\[\[)])*

but if there are any [ in the spaces that are not between [[ and ]] then they are excluded.

Just some background: The end user can select from a list of field codes that look like [[husband name]] or [[num kids]] and so on. These are fixed items that can be chosen and dropped into grid cells. The interstitial cells (so to speak) can be any text the user chooses to enter. I of course prevent an end user from entering [[ AND ]] into a user defined cell but I don''t want to prevent single "[" or "]" or even double "[[" OR double "]]". Since the field codes are absolutely limited to [a-zA-Z0-9\s] by me, and I can prevent [[ ]] from being entered by the end user, there will always be a distinction between the field codes and the end user entered data. If I have to prevent either [[ or ]] from being entered, I''ll do that but I''d rather not limit it.

推荐答案

允许不带括号的可选文本

Allow for optional text that does not have brackets in it

String yourExampleString = "Place the string you want to parse here";
String regexString = @"([^\[])*(\[\[[a-zA-Z0-9\s]*\]\])?";
Regex regex = new Regex(regexString);
Matches matches = regex.Match(yourExampleString);
foreach(Match match in matches)
{
    //Process the separate matches here
    //By accessing the Groups collection of the current match
    //you can get at the capture groups that were made by the parenthesis
    //in the regular expression
}



现在,您有两个捕获组:不带括号的可选前导文本和带括号的可选文本.匹配成功后,您要做的就是检查第一组内容的前导文本和第二组内容的方括号内容.您为所有比赛完成此操作,就完成了.

那应该做!

干杯,



Now you have two capture groups: optional leading text without brackets and optional text in brackets. When your match succeeds all you have to do is inspect the content of the first group for leading text and the second group for the bracketed content. You do this for all matches and you''re done.

That should do it!

Cheers,


好吧,我想我明白了.我做了自己的小vb.net正则表达式测试器和以下字符串:

"[[[丈夫名字]]已嫁给{} * []]] ^ 234/*-*-+ [夫人[[妻子名字]],并且他们有[[num kids]] kidsx"

使用以下模式:

(\ [\ [[a-zA-Z0-9 \ s] * \] \])

呈现以下结果:

"
"[[丈夫名]]"
已嫁给{} * []]]] ^ 234/*-*-+太太."
"[[妻子的名字]]"
",他们有"
"[[num kids]]"
"kidsx"

无论我为字符串输入什么,它都可以工作,所以我认为我已经准备好了.我需要添加到原始模式中的就是打开和关闭括号.如果有人可以告诉我为什么这可能不是解决方法,请告诉我.
Well I think I figured it out. I made my own little vb.net regex tester and this string:

"[[husband name]] is married to{}*[]]]]^234/*-*-+ Mrs. [[wife name]] and they have [[num kids]] kidsx "

with this pattern:

(\[\[[a-zA-Z0-9\s]*\]\])

renders these results:

""
"[[husband name]]"
" is married to{}*[]]]]^234/*-*-+ Mrs. "
"[[wife name]]"
" and they have "
"[[num kids]]"
" kidsx "

It works no matter what I enter for the string so I think I''m set. All I needed to add to my original pattern was the open and close parens. If anyone can tell me why this might not be the solution, let me know.


这篇关于需要一些模式帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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