作者DearKurt ("小朋友"会自己找出路...)
看板C_Sharp
标题Re: [问题] 取出字串中括号内容
时间Sat Oct 2 15:19:10 2010
※ 引述《QuestionTwo (QuestionTwo)》之铭言:
: string str = "王小明(00001)";
: 想要单独取出 00001 这个内容。
: 於是想到使用substring 看能不能达到
: 找了半天,後来使用了
: Regex reg = new Regex("[0-9]{1,}");
: string show = reg.Match(test).ToString();
: 这方法可以正确取出00001
: 但如果"王小明"变成"王小明2"就会取出"2"
: 如果写成:Regex reg = new Regex("\([0-9]{1,}\)");
: 就会把不要的括号也取出来: (00001)
用subexpression可以做到
Regex reg = new Regex("\\(([0-9]{1,})\\)");
string show = reg.Match(test).Groups[1].Value;
括符是保留字 用於将整个expression再细分成几个group
就可以把特定group的字取出
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 124.11.129.161