使用正则表达式检索两个括号之间的文本 [英] Using a regular expression to retrieve the text between two parentheses

查看:291
本文介绍了使用正则表达式检索两个括号之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




假设我有一个由一个人的名字组成的字符串,后跟括号中的

职业,例如


string strText =" Tiger Woods(高尔夫球手)" ;;


我想在括号中提取字符串的一部分

即高尔夫球手


正则表达式是否是最有效的方式...?


我我试图做这样的事情:


string strProfession = String.Empty;

Regex objRegEx = new Regex("((。 \ n)*?))",RegexOptions.IgnoreCase);

foreach(在objRegEx.Matches中匹配objMatch(strText)

{

strProfession = objMatch.ToString();

}


但是返回一个空字符串,毫无疑问因为我没有定义

正则表达式正确。


此外,甚至还需要在这里设置一个foreach循环,如同是

特殊场景只能有一场比赛...?


感激不尽的任何帮助。


Mark

Hi,

Supposing I had a string made up of a person''s name followed by their
profession in parentheses e.g.

string strText = "Tiger Woods (golfer)";

and I wanted to extract the portion of the string between the parentheses
i.e. "golfer"

Would a regular expression be the most efficient way of doing this...?

I''m trying to do something like this:

string strProfession = String.Empty;
Regex objRegEx = new Regex("(((.|\n)*?))", RegexOptions.IgnoreCase);
foreach (Match objMatch in objRegEx.Matches(strText)
{
strProfession = objMatch.ToString();
}

but that is returning an empty string, no doubt because I haven''t defined
the regular expression correctly.

Also, is it even necessary to have a foreach loop here, as in this
particular scenario there can only ever be one match...?

Any assistance gratefully received.

Mark

推荐答案

Mark Rae写道:
Mark Rae wrote:

假设我有一个字符串组成一个人的名字后面跟着他们的

专业括号,例如


string strText =" Tiger Woods(golfer);


我想在括号中提取字符串的部分

即高尔夫球手


定期表达式是最有效的方式......?


我正在尝试做这样的事情:


string strProfession = String.Empty;

Regex objRegEx = new Regex(&(;(。| \ n)*?))",RegexOptions.IgnoreCase);

foreach(在objRegEx.Matches中匹配objMatch(strText)

{

strProfession = objMatch.ToStr ing();

}


但是返回一个空字符串,毫无疑问因为我没有定义

正确的正则表达式。


此外,甚至还需要在这里设置一个foreach循环,因为在这个特殊情况下,只有一个匹配...?
Supposing I had a string made up of a person''s name followed by their
profession in parentheses e.g.

string strText = "Tiger Woods (golfer)";

and I wanted to extract the portion of the string between the parentheses
i.e. "golfer"

Would a regular expression be the most efficient way of doing this...?

I''m trying to do something like this:

string strProfession = String.Empty;
Regex objRegEx = new Regex("(((.|\n)*?))", RegexOptions.IgnoreCase);
foreach (Match objMatch in objRegEx.Matches(strText)
{
strProfession = objMatch.ToString();
}

but that is returning an empty string, no doubt because I haven''t defined
the regular expression correctly.

Also, is it even necessary to have a foreach loop here, as in this
particular scenario there can only ever be one match...?



string s =" Tiger Woods(golfer)";

Regex re = new Regex(@"(\( )([^ \]] *)(\))");

string prof = re.Match(s).Groups [2] .Value;


似乎有用。


没有正则表达式通常不是最有效的编码方式,

但这是简单的代码一个记录良好的语法。


一些使用IndexOf的spagetti会更快,但如果修改代码,它会更容易引入错误。


Arne

string s = "Tiger Woods (golfer)";
Regex re = new Regex(@"(\()([^\)]*)(\))");
string prof = re.Match(s).Groups[2].Value;

seems to work.

No regex will typical not be the most efficient way of coding it,
but it is simple code with a well documented syntax.

Some spagetti with IndexOf will be faster, but it would
also be much easier to introduce bugs if modifying the code.

Arne


Arne Vajh?j写道:
Arne Vajh?j wrote:

假设我有一个由一个人的名字组成的字符串,后跟括号中的

职业,例如


string strText = 老虎伍兹(高尔夫球手);


我想在括号中提取弦的一部分

即g olfer"


正则表达式是否是最有效的方式...?
Supposing I had a string made up of a person''s name followed by their
profession in parentheses e.g.

string strText = "Tiger Woods (golfer)";

and I wanted to extract the portion of the string between the parentheses
i.e. "golfer"

Would a regular expression be the most efficient way of doing this...?


Regex re = new Regex(@"(\()([^ \]] *)(\ ))");
Regex re = new Regex(@"(\()([^\)]*)(\))");



@" \(([^ \]] +)\)",RegexOptions.IgnorePatternWhitespace


可能更简单,更快 - 没有真正需要捕获

parens。

@"\( ([^\)]+) \)", RegexOptions.IgnorePatternWhitespace

is probably a bit simpler and faster - there''s no real need to capture
the parens.


没有正则表达式会是典型的不是最有效的编码方式,

但它是简单的代码,具有良好的文档记录。
No regex will typical not be the most efficient way of coding it,
but it is simple code with a well documented syntax.



你可能会感到惊讶。我比较了一个正则表达式,用手工编码的状态机找到

%符号(@"%(\ w +)%&)之间的所有令牌。

不仅手写代码需要花费大约180倍的时间来写(即,b / b $ 15分钟对比5秒)它也跑得慢了。一旦

任务变得复杂,正则表达式就可以节省程序员时间和运行时间

You might be surprised. I compared a regex to find all tokens between
% signs (@"% (\w+) %") with a hand-coded state machine. Not only did
the hand-coded version take about 180 times as long to write (ie,
fifteen minutes vs five seconds) it also ran slower. As soon as the
task gets at all complex, a regex can save both programmer time and
run time.


使用IndexOf的某些spagetti会更快,但如果修改代码,它会更容易引入错误。
Some spagetti with IndexOf will be faster, but it would
also be much easier to introduce bugs if modifying the code.



是的,正则表达式更容易阅读和维护 - 一行的一部分,

而不是三个语句和一些注释。


-


..NET 2.0 for Delphi程序员
www.midnightbeach.com/.net




string strText =" Tiger Woods(golfer)" ;;
string strText = "Tiger Woods (golfer)";



其他人提供了工作正则表达式 - 所以我不会重复。


但也许你应该知道

正则表达式中隐含的限制 - 主要通常呈现为正则表达式不能计数的限制。因为你不需要处理递归结构,嵌套

分隔符等等,所以正则表达式通常都能正常工作。但是它们不能被用来找到平衡支架,验证正确的嵌套等等。


从理论上讲,你可以明确地构建一个正则表达式来处理给定的最大数量的平衡分隔符对,但即使要处理单个

额外的nestiing级别也需要一个如此复杂的正则表达式模式只有明确地编码匹配算法才能更清晰,更简单。


当然,有一种思想认为正则表达式很少只是

最好的

解决方案。 -


有些人在面对问题时会想''我知道,我会用

正则表达式。''现在他们有两个问题。 - Jamie Zawinski {* 1]


欢呼,


gary

http://www.oxide.net.au


* [1 ] - 有关这句话起源的有趣讨论,请参阅

Jeffrey Friedl的博客
http://regex.info/blog/2006-09-15/247

Others have supplied working regexes - so I won''t repeat.

But perhaps you should be made aware of the limitations implicit in
regexes - the main one being commonly rendered as "regexes can''t count". As
long as you are not having to deal with recursive structures, nested
delimiters and so on, regexes will often work well. But they can''t be used
to "find the balancing brace", verify correct nesting or suchlike.

In theory, you can explicitly construct a regex to cope with a given maximum
number of pairs of balancing delimiters, but even to cope with a single
extra level of nestiing requires a regex pattern so complex that it''s
clearer and simpler to just code the match algorithm explicitly.

And of course there is the school of thought that regexes are only rarely
the best
solution. -

"Some people, when confronted with a problem, think ''I know, I''ll use
regular expressions.'' Now they have two problems." - Jamie Zawinski{*1]

cheers,

gary

http://www.oxide.net.au

*[1] - For an entertaining discussion of the origins of this quote, see
Jeffrey Friedl''s blog at
http://regex.info/blog/2006-09-15/247


这篇关于使用正则表达式检索两个括号之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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