获取括号内的文字 [英] Get text inside parentheses

查看:113
本文介绍了获取括号内的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字符串如下:

I have a string that contains the following:

test (alpha)

我想要得到的括号内的文字,这样我只有字母。这可以通过使用正则表达式,如实现\(([^)] *)\)。是否有更简单的方法?

I want to get the text inside the parentheses so that I only have alpha. This can be achieved using a regular expression such as \(([^)]*)\). Is there an easier way?

推荐答案

您正则表达式是容易的。如果你不希望创建两个指标是,是,如果你想做到这一点没有任何捕获组,那么你可以使用基于正则表达式环视像下面。

Your regex is an easy one. If you don't want to create two indexes that is, if you want to achieve this without any capturing group then you could use lookaround based regex like below.

Regex rgx = new Regex(@"(?<=\()[^()]*(?=\))");

演示


  • (小于?= \()正回顾后,其断言,这场比赛必须由开paranthesis

  • (?<=\() Positive lookbehind, which asserts that the match must be preceded by an opening paranthesis (.

[()] * 匹配任何字符,但不是零或多次。

[()]* Matches any character but not of ( or ) zero or more times.

(?= \))正向前查找它断言这场比赛必须跟一个收paranthesis。

(?=\)) Positive lookahead which asserts that the match must be followed by a ) closing paranthesis.

IDEONE

这篇关于获取括号内的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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