我如何提取圆括号(圆括号)之间是文字? [英] How do I extract text that lies between parentheses (round brackets)?

查看:155
本文介绍了我如何提取圆括号(圆括号)之间是文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串用户名(销售)我要提取的括号内的文字,我将如何做到这一点?

I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?

我怀疑的子串,但我不知道如何阅读,直到右括号,文字的长度会有所不同。

I suspect sub-string but I can't work out how to read until the closing bracket, the length of text will vary.

推荐答案

一个非常简单的方法来做到这一点是通过使用常规的前pressions:

A very simple way to do it is by using regular expressions:

Regex.Match("User name (sales)", @"\(([^)]*)\)").Groups[1].Value


至于(很搞笑)评论的回应,这是同样的正则表达式有一些解释:


As a response to the (very funny) comment, here's the same Regex with some explanation:

\(             # Escaped parenthesis, means "starts with a '(' character"
    (          # Parentheses in a regex mean "put (capture) the stuff 
               #     in between into the Groups array" 
       [^)]    # Any character that is not a ')' character
       *       # Zero or more occurrences of the aforementioned "non ')' char"
    )          # Close the capturing group
\)             # "Ends with a ')' character"

这篇关于我如何提取圆括号(圆括号)之间是文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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